kevlened / pytest-parallel

A pytest plugin for parallel and concurrent testing
https://github.com/browsertron/pytest-parallel/issues/104#issuecomment-1293941066
MIT License
313 stars 59 forks source link

do not create thread if tests_per_worker==1 #96

Open tovmeod opened 3 years ago

tovmeod commented 3 years ago

I have some tests using signal and I get" ` signalnum = <Signals.SIGINT: 2> handler =

@_wraps(_signal.signal)
def signal(signalnum, handler):
  handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))

E ValueError: signal only works in main thread`

To solve this I propose to not use a ThreadWorker if tests_per_worker==1 and just run it directly we can use the following instead of process_with_threads:

` def process_with_one_thread(config, queue, session, tests_per_worker, errors):

This function will be called from subprocesses, forked from the main

    # pytest process. First thing we need to do is to change config's value
    # so we know we are running as a worker.
    config.parallel_worker = True

    pickling_support.install()
      while True:
          try:
              index = queue.get()
              if index == 'stop':
                  queue.task_done()
                  break
          except ConnectionRefusedError:
              time.sleep(.1)
              continue
          item = session.items[index]
          try:
              pytest_parallel.run_test(session, item, None)
          except BaseException:
              import pickle
              import sys

              errors.put(('name', pickle.dumps(sys.exc_info())))
          finally:
              try:
                  queue.task_done()
              except ConnectionRefusedError:
                  pass

`