问题
I have python2.5 and multiprocessoring (get from http://code.google.com/p/python-multiprocessing/)
This simple code (get from docs), works very strange from time to time, sometimes it ok, but sometimes it throw timeout ex or hang my Windows (Vista), only reset helps :) Why this can happen?
from multiprocessing import Pool
def f(x):
print "fc",x
return x*x
pool = Pool(processes=4)
if __name__ == '__main__':
result = pool.apply_async(f, (10,)) # evaluate "f(10)" asynchronously
print result.get(timeout=3) # prints "100" unless your computer is *very* slow
回答1:
This is just a wild guess, but have you tried to move the Pool
creation into the if
block? I suspect that otherwise it might spawn an unlimited number of new processes, causing the freeze.
来源:https://stackoverflow.com/questions/3674746/python2-5-multiprocessing-pool