python中的多進程運用
- 2019 年 10 月 10 日
- 筆記
版權聲明:本文為部落客原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。
本文鏈接:https://blog.csdn.net/weixin_40313634/article/details/100937313
1. 程式中另起進程
from multiprocessing import Process from os import getpid from time import sleep def run_time(name): sleep(5) print('child progress {}: {}'.format(name, getpid())) if __name__ == '__main__': print('parent progress id: {}'.format(getppid())) # os.getpid(): 獲取當前進程ID; os.getppid(): 獲取父進程ID p = Process(target=run_time, args=('hello_world',)) # 初始化子進程:target=目前函數名, args=參數 print('start child progress') p.start() # 啟動子進程 p.join() # 進程同步:等待子進程執行完畢,程式再向下執行 print('end child progress')
方法
- getppid()、getpid()
- p.start()
- p.join()
- Process(target=, args=(,))
- Pool(n)
- p.apply_async(, args=(,))
- subprocess.call([』』, 『』])
- Queue()
- q.put(value)
- q.get(True)
2. 進程池
from multiprocessing import Pool from os import getpid from time import sleep def long_time_task(name): sleep(5) print('child process {0}: {1}n'.format(name, getpid())) if __name__ == '__main__': print('parent process: {0}n'.format(getpid())) p = Pool(2) ## 初始化進程池:一次可並行執行2個進程(等這兩個執行結束,再次啟動執行接下來的兩個,和電腦是幾核的無關) for i in range(10): p.apply_async(long_time_task, args=(i,)) ## 進程池添加進程 print('start child progressn') p.close() ## 關閉進程池(不能再添加新進程了),準備執行進程 p.join() # 進程同步 print('end child progressn')
3. 問題:無法獲取子進程的執行結果 -> 子進程
import subprocess print('nslookup www.python.org') r = subprocess.call(['nslookup', 'www.python.org']) print('Exit code: {}'.format(r))
4. 進程間通訊
- 隊列:Queue
from multiprocessing import Process, Queue from time import sleep from random import random def write(q): for i in range(5): print('puts {}'.format(i)) q.put(i) sleep(1) def read(q): sleep(10) while True: i = q.get(True) print('get {}'.format(i)) if __name__ == '__main__': q = Queue() pw = Process(target=write, args=(q,)) pr = Process(target=read, args=(q,)) print('START') pw.start() pr.start() pw.join() while True: if q.empty(): # 如果隊列中的數據都處理完了,則強制停止read方法 pr.terminate() break print('END')
- 訊號量:Semaphore
from multiprocessing import Process, Semaphore from time import sleep def write(f): f.acquire() sleep(5) f.release() if __name__ == '__main__': pp = [] f = Semaphore(1) for i in range(5): p = Process(target=write, args=(f,)) p.start() pp.append(p) for p in pp: p.join()