1

I'm using ProcessPoolExecutor on Windows 10. Python version is 3.9.5. When I press Ctrl+C twice the program hangs endless, even if I set a timeout.

with concurrent.futures.ProcessPoolExecutor() as executor:
    results = executor.map(Worker, iterable, timeout=5)
    try:
        for result in results:
            DoSomething(result)
    except Exception as exc:
        print(exc)
        executor.shutdown(wait=True, cancel_futures=True)

Error message is:

  ...
  File "C:\Python\foo.py", line 162, in FooFunc
    executor.shutdown(wait=True, cancel_futures=True)
  File "C:\Python\_envs\Python39\lib\concurrent\futures\_base.py", line 636, in __exit__
    self.shutdown(wait=True)
  File "C:\Python\_envs\Python39\lib\concurrent\futures\process.py", line 740, in shutdown
    self._executor_manager_thread.join()
  File "C:\Python\_envs\Python39\lib\threading.py", line 1033, in join
    self._wait_for_tstate_lock()
  File "C:\Python\_envs\Python39\lib\threading.py", line 1049, in _wait_for_tstate_lock
    elif lock.acquire(block, timeout):
KeyboardInterrupt

Martin Bammer
  • 537
  • 7
  • 19

1 Answers1

1

I've found a solution. A workaround. It is a bit strange, because I would expect that the Python standard implementation is already able to deal with Ctrl+C.

The solution is to add to the main process:

def handler(signum, frame):
    print('Signal handler called with signal', signum)


if __name__ == "__main__":
    import signal
    signal.signal(signal.SIGINT, handler)
Martin Bammer
  • 537
  • 7
  • 19