国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python3多進程 multiprocessing 模塊實例詳解

2020-02-15 21:47:09
字體:
來源:轉載
供稿:網友

本文實例講述了Python3多進程 multiprocessing 模塊。分享給大家供大家參考,具體如下:

多進程 Multiprocessing 模塊

multiprocessing 模塊官方說明文檔

Process 類

Process 類用來描述一個進程對象。創建子進程的時候,只需要傳入一個執行函數和函數的參數即可完成 Process 示例的創建。

star() 方法啟動進程,
join() 方法實現進程間的同步,等待所有進程退出。
close() 用來阻止多余的進程涌入進程池 Pool 造成進程阻塞。

multiprocessing.Process(group=None, target=None, name=None, args=(), kwargs={}, *, daemon=None)

target 是函數名字,需要調用的函數
args 函數需要的參數,以 tuple 的形式傳入

示例:

import multiprocessingimport osdef run_proc(name):  print('Child process {0} {1} Running '.format(name, os.getpid()))if __name__ == '__main__':  print('Parent process {0} is Running'.format(os.getpid()))  for i in range(5):    p = multiprocessing.Process(target=run_proc, args=(str(i),))    print('process start')    p.start()  p.join()  print('Process close')

結果:

Parent process 809 is Running
process start
process start
process start
process start
process start
Child process 0 810 Running
Child process 1 811 Running
Child process 2 812 Running
Child process 3 813 Running
Child process 4 814 Running
Process close

Pool

Pool 可以提供指定數量的進程供用戶使用,默認是 CPU 核數。當有新的請求提交到 Poll 的時候,如果池子沒有滿,會創建一個進程來執行,否則就會讓該請求等待。

- Pool 對象調用 join 方法會等待所有的子進程執行完畢
- 調用 join 方法之前,必須調用 close
- 調用 close 之后就不能繼續添加新的 Process 了

pool.apply_async

apply_async 方法用來同步執行進程,允許多個進程同時進入池子。

import multiprocessingimport osimport timedef run_task(name):  print('Task {0} pid {1} is running, parent id is {2}'.format(name, os.getpid(), os.getppid()))  time.sleep(1)  print('Task {0} end.'.format(name))if __name__ == '__main__':  print('current process {0}'.format(os.getpid()))  p = multiprocessing.Pool(processes=3)  for i in range(6):    p.apply_async(run_task, args=(i,))  print('Waiting for all subprocesses done...')  p.close()  p.join()  print('All processes done!')

結果:

current process 921
Waiting for all subprocesses done...
Task 0 pid 922 is running, parent id is 921
Task 1 pid 923 is running, parent id is 921
Task 2 pid 924 is running, parent id is 921
Task 0 end.
Task 3 pid 922 is running, parent id is 921
Task 1 end.
Task 4 pid 923 is running, parent id is 921

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 潮州市| 辽中县| 合肥市| 城固县| 千阳县| 芜湖市| 鲁甸县| 冀州市| 许昌县| 萍乡市| 五莲县| 云霄县| 当涂县| 香河县| 来凤县| 红原县| 徐州市| 苗栗市| 金溪县| 凤山市| 石台县| 岱山县| 徐闻县| 金平| 武强县| 耿马| 温泉县| 任丘市| 金寨县| 白山市| 泗水县| 黄浦区| 孝义市| 济南市| 西吉县| 万载县| 金湖县| 敦化市| 安乡县| 周至县| 秦皇岛市|