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

首頁 > 編程 > Python > 正文

探究Python多進程編程下線程之間變量的共享問題

2019-11-25 17:33:09
字體:
來源:轉載
供稿:網友

 1、問題:

群中有同學貼了如下一段代碼,問為何 list 最后打印的是空值?
 

from multiprocessing import Process, Managerimport 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

其實如果你了解 python 的多線程模型,GIL 問題,然后了解多線程、多進程原理,上述問題不難回答,不過如果你不知道也沒關系,跑一下上面的代碼你就知道是什么問題了。
 

python aa.pyprocess id: 632process id: 635process id: 637process id: 633process id: 636process id: 634process id: 639process id: 638process id: 641process id: 640------------------------process id: 619[]

將第 6 行注釋開啟,你會看到如下結果:
 

process id: 32074process id: 32073process id: 32072process id: 32078process id: 32076process id: 32071process id: 32077process id: 32079process id: 32075process id: 32080------------------------process id: 32066[3, 2, 1, 7, 5, 0, 6, 8, 4, 9]

2、python 多進程共享變量的幾種方式:
(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[:]

結果:
 

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.
代碼見開頭的例子。

http://docs.python.org/2/library/multiprocessing.html#managers
3、多進程的問題遠不止這么多:數據的同步

看段簡單的代碼:一個簡單的計數器:
 

from multiprocessing import Process, Managerimport 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

結果:
 

------------------------process id: 1737897

也許你會問:WTF?其實這個問題在多線程時代就存在了,只是在多進程時代又杯具重演了而已:Lock!
 

from multiprocessing import Process, Manager, Lockimport 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

這段代碼性能如何呢?跑跑看,或者加大循環次數試一下。。。
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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沂源县| 扎兰屯市| 泰来县| 泸溪县| 青州市| 同心县| 吉安县| 辽源市| 扎兰屯市| 和政县| 安国市| 龙口市| 中方县| 曲靖市| 精河县| 周至县| 东安县| 漳浦县| 绥棱县| 萨迦县| 慈利县| 沁水县| 沾化县| 长海县| 竹溪县| 平凉市| 贺兰县| 封丘县| 施秉县| 元阳县| 綦江县| 安西县| 德兴市| 兴安县| 扶绥县| 门头沟区| 湛江市| 肥城市| 科技| 义乌市| 岑巩县|