【发布时间】:2017-11-17 12:40:33
【问题描述】:
我正在尝试使用 multiprocessing.pool,它似乎工作正常,除了整个脚本一次又一次循环......
我的代码 -
from multiprocessing import Pool
print "at top!"
arr = [30, 30, 30, 30]
def fib(num):
if num == 0 or num == 1: return 1
else: return (fib(num - 1) + fib(num - 2))
def main():
print "start"
pool = Pool(processes=4)
result = [pool.apply_async(fib, args=(arr[i],)) for i in range(4)]
pool.close()
pool.join()
for res in result:
print res.get()
print "end"
if __name__ == '__main__':
main()
print "end of program ?"
它返回的是这个 -
at top!
start
at top!
end of program ?
at top!
end of program ?
at top!
end of program ?
at top!
end of program ?
1346269
1346269
1346269
1346269
end
end of program ?
我不想要的是重复的at top ! 和end of program。
我希望主程序在该点冻结并仅在所有计算完成后继续。
PS。我在 Windows 10、64 位上运行它。 Python 2.7 32 位。
提前致谢。
【问题讨论】:
-
奇怪,当我运行这个(在python2.7中)时,我只得到
at top和end of program ?一次。
标签: python windows multiprocessing python-multiprocessing