multiprocessing: child process race to answer

cappleman at gmail.com cappleman at gmail.com
Sun Nov 3 05:10:25 EST 2013


On Friday, November 1, 2013 10:35:47 PM UTC-4, smhall05 wrote:
> I am using a basic multiprocessing snippet I found:
> 
> 
> 
> #-----------------------------------------------------
> 
> from multiprocessing import Pool
> 
> 
> 
> def  f(x):
> 
>     return x*x
> 
> 
> 
> if __name__ == '__main__':
> 
>     pool = Pool(processes=4)              # start 4 worker processes
> 
>     result = pool.apply_async(f, [10])    # evaluate "f(10)" asynchronously
> 
>     print result.get(timeout=1)           
> 
>     print pool.map(f, range(10))          # prints "[0, 1, 4,..., 81]"
> 
> #---------------------------------------------------------
> 
> 
> 
> I am using this code to have each process go off and solve the same problem, just with different inputs to the problem. I need to be able to kill all processes once 1 of n processes has come up with the solution. There will only be one answer.
> 
> 
> 
> I have tried:
> 
> 
> 
> sys.exit(0) #this causes the program to hang
> 
> pool.close()
> 
> pool.terminate
> 
> 
> 
> These still allow further processing before the program terminates. What else can I try? I am not able to share the exact code at this time. I can provide more detail if I am unclear. Thank you

You could take a look at the Mutiprocessing module capabilities for exchanging objects between processes:

http://docs.python.org/3.3/library/multiprocessing.html#exchanging-objects-between-processes



More information about the Python-list mailing list