if __name__ == "__main__": pool = multiprocessing.Pool(processes=3) last_time = time.time() for i in range(4): msg = "hello %d" %(i) pool.apply_async(func, (msg, )) #维持执行的进程总数为processes,当一个进程执行完毕后会添加新的进程进去
if __name__ == "__main__": pool = multiprocessing.Pool(processes=3) last_time = time.time() for i in range(4): msg = "hello %d" %(i) pool.apply(func, (msg, )) #维持执行的进程总数为processes,当一个进程执行完毕后会添加新的进程进去
if __name__ == "__main__": pool = multiprocessing.Pool(processes=3) result = [] last_time = time.time() for i in range(4): msg = "hello %d" %(i) result.append(pool.apply_async(func, (msg, ))) print("run Main function") pool.close() pool.join() for res in result: print(":::", res.get()) print("Sub-process(es) done.") print("Cost time:", time.time() - last_time)
运行结果为:
1 2 3 4 5 6 7
run Main function ::: donehello 0 ::: donehello 1 ::: donehello 2 ::: donehello 3 Sub-process(es) done. Cost time: 6.130031585693359
defFrank(): print("\nRun task Frank-%s" %(os.getpid())) start = time.time() time.sleep(random.random() * 20) end = time.time() print('Task Frank runs %0.2f seconds.' %(end - start)) if __name__=='__main__': function_list= [Lee, Marlon, Allen, Frank] print("parent process %s" %(os.getpid()))
pool=multiprocessing.Pool(4) for func in function_list: pool.apply_async(func) #Pool执行函数,apply执行函数,当有一个进程执行完毕后,会添加一个新的进程到pool中
print('Waiting for all subprocesses done...') pool.close() pool.join() #调用join之前,一定要先调用close() 函数,否则会出错, close()执行后不会有新的进程加入到pool,join函数等待素有子进程结束 print('All subprocesses done.')
运行结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
parent process 24276 Waiting for all subprocesses done...
Run task Lee-16688 Run task Marlon-27824
Run task Allen-2188
Run task Frank-27380 Task Marlon runs 2.47 seconds. Task Frank runs 5.59 seconds. Task Lee, runs 6.12 seconds. Task Allen runs 19.02 seconds. All subprocesses done.
map用法
代码:
1 2 3 4 5 6 7 8 9 10
#coding: utf-8 import multiprocessing
defm1(x): print(x * x)
if __name__ == '__main__': pool = multiprocessing.Pool(multiprocessing.cpu_count()) i_list = range(8) pool.map(m1, i_list)
运行结果:
1 2 3 4 5 6 7 8
0 1 4 25 36 9 16 49
但是代码若改成这样的形式,得到的值就是有顺序的:
1 2 3 4 5 6 7 8 9 10 11
#coding: utf-8 import multiprocessing
defm1(x): return x * x
if __name__ == '__main__': pool = multiprocessing.Pool(multiprocessing.cpu_count()) i_list = range(8) res = pool.map(m1, i_list) print(res)
运行结果:
1
[0, 1, 4, 9, 16, 25, 36, 49]
若要使用apply_async完成该功能
代码:
1 2 3 4 5 6 7 8 9 10
#coding: utf-8 import multiprocessing
defm1(x): print(x * x)
if __name__ == '__main__': pool = multiprocessing.Pool(multiprocessing.cpu_count()) multi_res = [pool.apply_async(m1, (i,)) for i in range(8)] print([res.get() for res in multi_res])
运行结果:
1 2 3 4 5 6 7 8
0 4 1 9 36 25 16 49
同样代码若改成这样的形式,得到的值就是有顺序的:
1 2 3 4 5 6 7 8 9 10
#coding: utf-8 import multiprocessing
defm1(x): return x * x
if __name__ == '__main__': pool = multiprocessing.Pool(multiprocessing.cpu_count()) multi_res = [pool.apply_async(m1, (i,)) for i in range(8)] print([res.get() for res in multi_res])
if __name__ == "__main__": p = Pool(5) pool_output = p.map(hello, range(3))
print(pool_output)
运行结果为:
1 2 3 4 5 6 7 8 9 10 11 12 13
hi outside of main() hi outside of main() hi outside of main() inside hello() Proccess id: 31816 hi outside of main() inside hello() Proccess id: 32888 inside hello() Proccess id: 34140 hi outside of main() hi outside of main() [0, 1, 4]
当使用ThreadPool,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
from multiprocessing.pool import ThreadPool import os, time