问题
Why in the example below myFunct() is not printing any messages is supposed to when it is ran by 'multiprocessing'? And how to solve it?
import multiprocessing as mp
poolDict=mp.Manager().dict()
def myFunct(arg):
print 'myFunct():', arg
for i in range(110):
for n in range(500000):
pass
poolDict[arg]=i
print 'myFunct(): completed', arg, poolDict
from multiprocessing import Pool
pool = Pool(processes=2)
myArgsList=['arg1','arg2','arg3']
pool.map_async( myFunct, myArgsList)
print 'completed'
回答1:
This isn't printing because the main process is exiting immediately after calling map_async
, so none of the child processes have a chance to actually run. It prints as expected if you make the script wait for the children to finish:
pool.map_async( myFunct, myArgsList)
pool.close()
pool.join()
print 'completed'
Output from bash prompt:
dan@dan:~> ./mult.py
myFunct(): arg1
myFunct(): arg2
myFunct(): completed arg2 {'arg1': 108, 'arg2': 109}
myFunct(): arg3
myFunct(): completed arg1 {'arg1': 109, 'arg2': 109}
myFunct(): completed arg3 {'arg1': 109, 'arg2': 109, 'arg3': 109}
completed
回答2:
It works fine if you use apply_async() instead of map_async() and add the close() and join() calls:
import multiprocessing as mp
poolDict=mp.Manager().dict()
def myFunct(arg):
print 'myFunct():', arg
for i in range(110):
for n in range(500000):
pass
poolDict[arg]=i
print 'myFunct(): completed', arg, poolDict
from multiprocessing import Pool
pool = Pool(processes=2)
myArgsList=['arg1','arg2','arg3']
pool.apply_async( myFunct, myArgsList)
pool.close()
pool.join()
print 'completed'
Result:
myFunct(): arg1
myFunct(): arg2
myFunct(): completedmyFunct(): completed arg1 {'arg1': 109, 'arg2': 109}
arg2 {'arg1': 108, 'arg2': 109}
myFunct(): arg3
myFunct(): completed arg3 {'arg1': 109, 'arg2': 109, 'arg3': 109}
completed
来源:https://stackoverflow.com/questions/23961316/why-function-called-by-multiprocessing-is-not-printing-the-messages