簡單談?wù)刾ython中的多進程
進程是由系統(tǒng)自己管理的。
1:最基本的寫法
from multiprocessing import Pool def f(x): return x*x if __name__ == '__main__': p = Pool(5) print(p.map(f, [1, 2, 3])) [1, 4, 9]
2、實際上是通過os.fork的方法產(chǎn)生進程的
unix中,所有進程都是通過fork的方法產(chǎn)生的。
multiprocessing Process os info(title): title , __name__ (os, ): , os.getppid() , os.getpid() f(name): info() , name __name__ == : info() p = Process(=f, =(,)) p.start() p.join()
3、線程共享內(nèi)存
threading run(info_list,n): info_list.append(n) info_list __name__ == : info=[] i (): p=threading.Thread(=run,=[info,i]) p.start() [0] [0, 1] [0, 1, 2] [0, 1, 2, 3] [0, 1, 2, 3, 4] [0, 1, 2, 3, 4, 5] [0, 1, 2, 3, 4, 5, 6] [0, 1, 2, 3, 4, 5, 6, 7] [0, 1, 2, 3, 4, 5, 6, 7, 8] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
進程不共享內(nèi)存:
multiprocessing Process run(info_list,n): info_list.append(n) info_list __name__ == : info=[] i (): p=Process(=run,=[info,i]) p.start() [1] [2] [3] [0] [4] [5] [6] [7] [8] [9]
若想共享內(nèi)存,需使用multiprocessing模塊中的Queue
multiprocessing Process, Queue f(q,n): q.put([n,]) __name__ == : q=Queue() i (): p=Process(=f,=(q,i)) p.start() : q.get()
4、鎖:僅是對于屏幕的共享,因為進程是獨立的,所以對于多進程沒有用
multiprocessing Process, Lock f(l, i): l.acquire() , i l.release() __name__ == : lock = Lock() num (): Process(=f, =(lock, num)).start() hello world 0 hello world 1 hello world 2 hello world 3 hello world 4 hello world 5 hello world 6 hello world 7 hello world 8 hello world 9
5、進程間內(nèi)存共享:Value,Array
multiprocessing Process, Value, Array f(n, a): n.value = i ((a)): a[i] = -a[i] __name__ == : num = Value(, ) arr = Array(, ()) num.value arr[:] p = Process(=f, =(num, arr)) p.start() p.join() 0.0 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 3.1415927 [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
#manager共享方法,但速度慢
multiprocessing Process, Manager f(d, l): d[] = d[] = d[] = l.reverse() __name__ == : manager = Manager() d = manager.dict() l = manager.list(()) p = Process(=f, =(d, l)) p.start() p.join() d l # print '-------------'這里只是另一種寫法 # print pool.map(f,range(10)) {0.25: None, 1: '1', '2': 2} [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
#異步:這種寫法用的不多
multiprocessing Pool time f(x): x*x time.sleep() x*x __name__ == : pool=Pool(=) res_list=[] i (): res=pool.apply_async(f,[i]) res_list.append(res) r res_list: r.get(timeout=10) #超時時間
同步的就是apply
相關(guān)文章
python中判斷數(shù)字是否為質(zhì)數(shù)的實例講解
在本篇文章里小編給大家分享了關(guān)于python中判斷數(shù)字是否為質(zhì)數(shù)的實例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2020-12-12用pip給python安裝matplotlib庫的詳細(xì)教程
這篇文章主要介紹了用pip給python安裝matplotlib庫的詳細(xì)教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02win10系統(tǒng)下Anaconda3安裝配置方法圖文教程
這篇文章主要為大家詳細(xì)介紹了win10系統(tǒng)下Anaconda3安裝配置方法圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-09-09Python基于socket實現(xiàn)簡單的即時通訊功能示例
這篇文章主要介紹了Python基于socket實現(xiàn)簡單的即時通訊功能,涉及Python基于socket模塊實現(xiàn)tcp通信客戶端與服務(wù)器端相關(guān)操作技巧,需要的朋友可以參考下2018-01-01Python?中的反轉(zhuǎn)字符串reversed(),切片
這篇文章主要介紹了Python?中的反轉(zhuǎn)字符串reversed(),切片?,以相反的順序反轉(zhuǎn)和處理字符串可能是編程中的一項常見任務(wù)。Python?提供了一組工具和技術(shù),可以幫助我們快速有效地執(zhí)行字符串反轉(zhuǎn),下面來看看具體內(nèi)容吧2021-12-12Django如何實現(xiàn)RBAC權(quán)限管理
這篇文章主要介紹了Django如何實現(xiàn)RBAC權(quán)限管理問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12