Multiprocessing - 图1
© getcodify.com

由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

Multiprocessing

Quick Start

  1. import multiprocessing as mp
  2. from time import sleep
  3. def echo(i):
  4. '''Working Function for cycle'''
  5. sleep(i)
  6. print(i)
  7. def multicore(Pool=10):
  8. pool = mp.Pool(processes=Pool)
  9. for i in range(10):
  10. # Working function "echo" and the arg 'i'
  11. multi_res = [pool.apply_async(echo,(i,))]
  12. pool.close()
  13. pool.join()
  14. if __name__ == '__main__':
  15. multicore()

With Return

Cite: StackoverFlow

  1. import multiprocessing as mp
  2. def worker(procnum, return_dict):
  3. '''worker function'''
  4. print (str(procnum) + ' represent!')
  5. return_dict[procnum] = procnum
  6. if __name__ == '__main__':
  7. manager = mp.Manager()
  8. return_dict = manager.dict()
  9. jobs = []
  10. for i in range(5):
  11. p = mp.Process(target=worker, args=(i,return_dict))
  12. jobs.append(p)
  13. p.start()
  14. for proc in jobs:
  15. proc.join()
  16. print (return_dict.values())
  17. for i in return_dict.values():
  18. print(i)

Enjoy~

本文由Python腳本GitHub/語雀自動更新

由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

GitHub: Karobben
Blog:Karobben
BiliBili:史上最不正經的生物狗