I'm trying to speed up a simple Python program using multiprocessing's Pool. Specifically: the imap_unordered function.
In my case I'm searching for a specific object with specific properties, and checking this property takes a long time, hence the reason I want to spread the load over my CPU cores.
I created the following code:
from multiprocessing import Pool as ThreadPool
pool = ThreadPool(4)
some_iterator = (create_item() for _ in range(100000))
results = pool.imap_unordered(my_function, some_iterator)
for result in results:
if is_favourable(result):
break
Unfortunately, after calling break, there is still a lot of activity in the threads (as can be observed in my computers activity monitor). How should I keep searching for results till I find a favourable one, or how can I stop iterating over all items using the imap_unordered iterator?