我已经写了一个使用多进程的Python程序。该程序调用8个工作进程,在睡眠3秒后输出一个随机数。我期望程序在3秒内完成,但实际上需要24秒才能完成,就好像每个工作进程函数是按顺序而不是并行评估的。有什么想法吗?
import time
import numpy as np
import multiprocessing as mp
import time
import sys
def f(i):
np.random.seed(int(time.time()+i))
time.sleep(3)
res=np.random.rand()
print "From i = ",i, " res = ",res
if __name__=='__main__':
num_workers=mp.cpu_count() # My CPu has 8 cores.
pool=mp.Pool(num_workers)
for i in range(num_workers):
p=pool.apply_async(f, args=(i,))
p.get()
pool.close()
pool.join()
然而,如果我使用Process而不是Pool,我会得到预期的正确结果:
import time
import numpy as np
import multiprocessing as mp
import time
import sys
def f(i):
np.random.seed(int(time.time()+i))
time.sleep(3)
res=np.random.rand()
print "From i = ",i, " res = ",res
if res>0.7:
print "find it"
if __name__=='__main__':
num_workers=mp.cpu_count()
pool=mp.Pool(num_workers)
for i in range(num_workers):
p=mp.Process(target=f,args=(i,))
p.start()
pool.map(f, range(num_workers))
,它会为你收集结果。 - Blckknghtapply_async()
比map()
更有意义。 但是,我当然不知道那个;-) - Tim Peters