I want to kill all processes if I have an exception in a process. Here is the trial code.
import multiprocessing as mp;
import time;
def f(x):
if x==1:
raise Exception("job error");
time.sleep(x*0.2);
print x;
return x*x;
if __name__ == '__main__':
p = mp.Pool(processes=5);
try:
for i in [4,6,3,5,1]:
p.apply_async(f, (i,));
except Exception as e: # exception is not caught!
print "catch",e;
p.terminate();
p.close();
p.join();
However, the exception is not caught. How can I catch the exception in a process and terminate all processes? I'm using python 2.7.