I have used the following code as advised but still the pool is not terminated after first worker is finding a match. The desired result is to have 20 processes running function f and when any of them finds a match the pool should be terminated, what is happening instead is that I have to wait until all workers find a match.
from multiprocessing import Pool
import numpy as np
def f(z):..Output omitted
class Worker():
def __init__(self, workers):
self.pool = Pool(processes=workers)
def callback(self, result):
if result:
self.pool.terminate()
def do_job(self):
self.pool.map_async(f,range(1,20), callback=self.callback)
self.pool.close()
self.pool.join()
if __name__=="__main__":
w = Worker(20)
w.do_job()