I am trying to calculate a value, I do not know the value, but the worker will return True once it’s done. But I cannot cancel the remaining jobs, they keep running forever.
Things I have tried:
executor.shutdown()
multiprocessing.Event()
multiprocessing.Queue()
executor.map() and executor.submit()
Am I misusing it, and this is not supposed to be possible? I am essentially trying to do this, but with multiple processes. However as noted, I cannot break out of the job.
for i in range(100000):
if i == 12345:
break
Ideally I want to use executor.map() with a ProcessPoolExecutor.
I am still taking advice, if there are any improvements. However this is my solution for future readers.
def worker(args, event):
if not event.is_set():
break
# do stuff
with multiprocessing.Manager() as manager:
event = manager.Event()
event.set()
with ProcessPoolExecutor() as executor:
futures = [executor.submit(worker, i, event) for i in iterable]
for future in futures:
if future.result():
print(future.result())
event.clear()
break