探究Python多進(jìn)程編程下線程之間變量的共享問(wèn)題
1、問(wèn)題:
群中有同學(xué)貼了如下一段代碼,問(wèn)為何 list 最后打印的是空值?
from multiprocessing import Process, Manager import os manager = Manager() vip_list = [] #vip_list = manager.list() def testFunc(cc): vip_list.append(cc) print 'process id:', os.getpid() if __name__ == '__main__': threads = [] for ll in range(10): t = Process(target=testFunc, args=(ll,)) t.daemon = True threads.append(t) for i in range(len(threads)): threads[i].start() for j in range(len(threads)): threads[j].join() print "------------------------" print 'process id:', os.getpid() print vip_list
其實(shí)如果你了解 python 的多線程模型,GIL 問(wèn)題,然后了解多線程、多進(jìn)程原理,上述問(wèn)題不難回答,不過(guò)如果你不知道也沒(méi)關(guān)系,跑一下上面的代碼你就知道是什么問(wèn)題了。
python aa.py process id: 632 process id: 635 process id: 637 process id: 633 process id: 636 process id: 634 process id: 639 process id: 638 process id: 641 process id: 640 ------------------------ process id: 619 []
將第 6 行注釋開(kāi)啟,你會(huì)看到如下結(jié)果:
process id: 32074 process id: 32073 process id: 32072 process id: 32078 process id: 32076 process id: 32071 process id: 32077 process id: 32079 process id: 32075 process id: 32080 ------------------------ process id: 32066 [3, 2, 1, 7, 5, 0, 6, 8, 4, 9]
2、python 多進(jìn)程共享變量的幾種方式:
(1)Shared memory:
Data can be stored in a shared memory map using Value or Array. For example, the following code
http://docs.python.org/2/library/multiprocessing.html#sharing-state-between-processes
from multiprocessing import Process, Value, Array def f(n, a): n.value = 3.1415927 for i in range(len(a)): a[i] = -a[i] if __name__ == '__main__': num = Value('d', 0.0) arr = Array('i', range(10)) p = Process(target=f, args=(num, arr)) p.start() p.join() print num.value print arr[:]
結(jié)果:
3.1415927 [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
(2)Server process:
A manager object returned by Manager() controls a server process which holds Python objects and allows other processes to manipulate them using proxies.
A manager returned by Manager() will support types list, dict, Namespace, Lock, RLock, Semaphore, BoundedSemaphore, Condition, Event, Queue, Value and Array.
代碼見(jiàn)開(kāi)頭的例子。
http://docs.python.org/2/library/multiprocessing.html#managers
3、多進(jìn)程的問(wèn)題遠(yuǎn)不止這么多:數(shù)據(jù)的同步
看段簡(jiǎn)單的代碼:一個(gè)簡(jiǎn)單的計(jì)數(shù)器:
from multiprocessing import Process, Manager import os manager = Manager() sum = manager.Value('tmp', 0) def testFunc(cc): sum.value += cc if __name__ == '__main__': threads = [] for ll in range(100): t = Process(target=testFunc, args=(1,)) t.daemon = True threads.append(t) for i in range(len(threads)): threads[i].start() for j in range(len(threads)): threads[j].join() print "------------------------" print 'process id:', os.getpid() print sum.value
結(jié)果:
------------------------ process id: 17378 97
也許你會(huì)問(wèn):WTF?其實(shí)這個(gè)問(wèn)題在多線程時(shí)代就存在了,只是在多進(jìn)程時(shí)代又杯具重演了而已:Lock!
from multiprocessing import Process, Manager, Lock import os lock = Lock() manager = Manager() sum = manager.Value('tmp', 0) def testFunc(cc, lock): with lock: sum.value += cc if __name__ == '__main__': threads = [] for ll in range(100): t = Process(target=testFunc, args=(1, lock)) t.daemon = True threads.append(t) for i in range(len(threads)): threads[i].start() for j in range(len(threads)): threads[j].join() print "------------------------" print 'process id:', os.getpid() print sum.value
這段代碼性能如何呢?跑跑看,或者加大循環(huán)次數(shù)試一下。。。
4、最后的建議:
Note that usually sharing data between processes may not be the best choice, because of all the synchronization issues; an approach involving actors exchanging messages is usually seen as a better choice. See also Python documentation: As mentioned above, when doing concurrent programming it is usually best to avoid using shared state as far as possible. This is particularly true when using multiple processes. However, if you really do need to use some shared data then multiprocessing provides a couple of ways of doing so.
5、Refer:
http://stackoverflow.com/questions/14124588/python-multiprocessing-shared-memory
http://eli.thegreenplace.net/2012/01/04/shared-counter-with-pythons-multiprocessing/
http://docs.python.org/2/library/multiprocessing.html#multiprocessing.sharedctypes.synchronized
相關(guān)文章
Python實(shí)現(xiàn)批量word文檔轉(zhuǎn)pdf并統(tǒng)計(jì)其頁(yè)碼
pypdf2是一個(gè)Python模塊,可以用來(lái)讀取、寫(xiě)入和操作PDF文件,本文就將利用該模塊實(shí)現(xiàn)批量word文檔轉(zhuǎn)pdf并統(tǒng)計(jì)其頁(yè)碼,需要的小伙伴可以了解一下2023-05-05Python編寫(xiě)一個(gè)驗(yàn)證碼圖片數(shù)據(jù)標(biāo)注GUI程序附源碼
這篇文章主要介紹了Python編寫(xiě)一個(gè)驗(yàn)證碼圖片數(shù)據(jù)標(biāo)注GUI程序,本文給大家附上小編精心整理的源碼,需要的朋友可以參考下2019-12-12基于Python實(shí)現(xiàn)視頻的人臉融合功能
這篇文章主要介紹了用Python快速實(shí)現(xiàn)視頻的人臉融合功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06使用Django的JsonResponse返回?cái)?shù)據(jù)的實(shí)現(xiàn)
這篇文章主要介紹了使用Django的JsonResponse返回?cái)?shù)據(jù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01如何利用Python實(shí)現(xiàn)n*n螺旋矩陣
這篇文章主要給大家介紹了關(guān)于如何利用Python實(shí)現(xiàn)n*n螺旋矩陣的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01將TensorFlow的模型網(wǎng)絡(luò)導(dǎo)出為單個(gè)文件的方法
本篇文章主要介紹了將TensorFlow的網(wǎng)絡(luò)導(dǎo)出為單個(gè)文件的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04TensorFlow可視化工具TensorBoard默認(rèn)圖與自定義圖
這篇文章主要介紹了TensorFlow可視化工具TensorBoard默認(rèn)圖與自定義圖的使用操作示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10簡(jiǎn)單了解python中的f.b.u.r函數(shù)
這篇文章主要介紹了簡(jiǎn)單了解python中的f.b.u.r函數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11