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

首頁 > 編程 > Python > 正文

Python 多進程

2019-11-06 06:50:16
字體:
來源:轉載
供稿:網友

1.os.fork()

普通的函數調用,調用一次,返回一次,但是fork()調用一次,返回兩次,因為操作系統自動把當前進程(稱為父進程)復制了一份(稱為子進程),然后,分別在父進程和子進程內返回。

子進程永遠返回0,而父進程返回子進程的ID。這樣做的理由是,一個父進程可以fork出很多子進程,所以,父進程要記下每個子進程的ID,而子進程只需要調用getppid()就可以拿到父進程的ID。 Python的os模塊封裝了常見的系統調用,其中就包括fork,可以在Python程序中輕松創建子進程:

import os輸出結果:

Process (3898) start… I (3898) just created a child process (3899). I am child process (3899) and my parent is 3898.

在Windows平臺中沒有fork調用,上面的代碼在Windows上無法運行。

2.multiprocessing

from multiprocessing import Processimport osdef run_proc(name): print 'Run child process %s (%s)' %(name,os.getpid())if __name__=='__main__': print 'Parent process %s.' % os.getpid() p = Process(target = run_proc, args=('test',)) print 'Process will start!' p.start() p.join() print 'Process end.'

輸出結果如下:

Parent process 3640. Process will start! Run child process test (3961) Process end.

3.Pool

如果要啟動大量的子進程,可以用進程池的方式批量創建子進程:

from multiprocessing import Poolimport os, time, randomdef long_time_task(name): print 'Run task %s (%s)...' % (name, os.getpid()) start = time.time() time.sleep(random.random() * 3) end = time.time() print 'Task %s runs %0.2f seconds.' % (name, (end - start))if __name__=='__main__': print 'Parent process %s.' % os.getpid() p = Pool(8) for i in range(9): p.apply_async(long_time_task, args=(i,)) print 'Waiting for all subprocesses done...' p.close() p.join() print 'All subprocesses done.'

輸出結果如下:

Parent process 3640. Run task 0 (3912)… Run task 1 (3913)… Run task 3 (3915)… Run task 2 (3914)… Run task 4 (3916)… Run task 6 (3918)… Run task 5 (3917)… Run task 7 (3919)… Task 4 runs 0.06 seconds. Run task 8 (3916)… Waiting for all subprocesses done… Task 5 runs 0.21 seconds. Task 0 runs 0.99 seconds. Task 6 runs 1.07 seconds. Task 3 runs 1.17 seconds. Task 7 runs 2.37 seconds. Task 1 runs 2.48 seconds. Task 8 runs 2.50 seconds. Task 2 runs 2.77 seconds. All subprocesses done.

4.Queue

Process之間肯定是需要通信的,操作系統提供了很多機制來實現進程間的通信。Python的multiprocessing模塊包裝了底層的機制,提供了Queue、Pipes等多種方式來交換數據。 以Queue為例,在父進程中創建兩個子進程,一個往Queue里寫數據,一個從Queue里讀數據:

from multiprocessing import Process,Queueimport os,time,randomdef write(q): for value in ['A','B','C']: print 'Put %s to queue...' %value q.put(value) time.sleep(random.random())def read(q): while True: value = q.get(True) print 'Get %s from queue.' %valueif __name__=='__main__': q = Queue() pw = Process(target = write,args=(q,)) pr = Process(target = read,args=(q,)) pw.start() pr.start() pw.join() pr.terminate()

輸出結果:

Put A to queue… Get A from queue. Put B to queue… Get B from queue. Put C to queue… Get C from queue.

參考: http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0013868323401155ceb3db1e2044f80b974b469eb06cb43000 《python核心編程》


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 手游| 湖口县| 钦州市| 招远市| 沐川县| 双峰县| 仁布县| 沙河市| 洪江市| 东海县| 阿图什市| 芦山县| 祁门县| 枞阳县| 修武县| 建平县| 德清县| 龙里县| 呼和浩特市| 隆子县| 黄浦区| 万荣县| 东平县| 麻城市| 神池县| 怀安县| 石狮市| 南陵县| 洪泽县| 巩留县| 南皮县| 贞丰县| 湘乡市| 京山县| 隆子县| 凤山市| 墨脱县| 泗水县| 山阴县| 龙胜| 那曲县|