<随便写>同步,异步进程池,线程

it2022-05-05  176

from multiprocessing import Pool import time import os def work(n): print("%s run" % os.getpid()) time.sleep(1) return n ** 2 if __name__ == '__main__': p = Pool(5) res_l = [] for i in range(10): # res = p.apply(work, args=(i,)) #同步 res = p.apply_async(work, args=(i,)) #异步 res_l.append(res) p.close() p.join() for res in res_l: print(res.get())

  

 

from threading import Thread import time import os def work(n): print("%s run" % os.getpid()) time.sleep(1) return n ** 2 if __name__ == '__main__': res_l = [] for i in range(10): t = Thread(target=work, args=(i,)) res_l.append(t) t.start() for res in res_l: print(res) print("主进程")

  

转载于:https://www.cnblogs.com/shuimohei/p/10500299.html


最新回复(0)