标签:multi async rgs import 进程池 进程 imp for a*
1、queueimport threading as td
import multiprocessing as mp
def job(q,a,b):
q.put(a**b)
if __name__ == "__main__":
q=mp.Queue()
t2=mp.Process(target=job,args=(q,3,3))
t1=mp.Process(target=job,args=(q,4,3))
t2.start()
t2.join()
t1.start()
t1.join()
res1=q.get()
res2=q.get()
print(res1)
print(res2)
2、进程池
import threading as td
import multiprocessing as mp
def job(x):
return x*x
def multicore():
pool=mp.Pool()
res=pool.map(job,range(100))
print(res)
if __name__ == "__main__":
multicore()
3、apply_async
import threading as td
import multiprocessing as mp
from time import *
def job(x):
return x*x
def multicore():
pool=mp.Pool(processes=3)
res=pool.map(job,range(4))
print(res)
res=pool.apply_async(job,(2,))
print(res)
multi_res=[pool.apply_async(job,(i,)) for i in range(12)]
print([resa.get() for resa in multi_res])
if __name__ == "__main__":
t1=time()
multicore()
t2=time()
print(t2-t1)
标签:multi async rgs import 进程池 进程 imp for a*
原文地址:https://blog.51cto.com/14156081/2439975