Search code examples
pythonmultiprocessingexecutorprocess-pool

Killing processes in ProcessPoolExecutor


I am using Python's ProcessPoolExecutor to run multiple processes in parallel and process them as any of them finishes. Then I look at their output and as soon as at least one of them gives satisfying answer I want to exit the program.

However, this is not possible since upon calling pool.shutdown(wait=False) I will have to wait for all active tasks in the pool to finish before I can exit my script.

Is there a way to kill all the remaining active children and exit? Also, is there a better way to stop as soon as at least one child returns the answer we are waiting for?


Solution

  • What you're doing is not quite clear, but executors strike me as the wrong tool entirely.

    multiprocessing.Pool seems much more suitable, and allows doing exactly what you're asking for: you can iterate on imap_unordered (or apply_async and poll the results), and once you have what you were looking for just terminate() the pool then join() it.