探究Python多进程编程下线程之间变量的共享问题
作者:xrzs 发布时间:2023-09-27 15:42:47
1、问题:
群中有同学贴了如下一段代码,问为何 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
其实如果你了解 python 的多线程模型,GIL 问题,然后了解多线程、多进程原理,上述问题不难回答,不过如果你不知道也没关系,跑一下上面的代码你就知道是什么问题了。
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 行注释开启,你会看到如下结果:
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 多进程共享变量的几种方式:
(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, 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
结果:
------------------------
process id: 17378
97
也许你会问:WTF?其实这个问题在多线程时代就存在了,只是在多进程时代又杯具重演了而已: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
这段代码性能如何呢?跑跑看,或者加大循环次数试一下。。。
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


猜你喜欢
- 前言本文主要给大家介绍了关于Golang实现字符串倒序的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍:字符串倒置如
- 1 引言在进行图像处理过程中,我们经常会遇到一些和物体轮廓相关的操作,比如求目标轮廓的周长面积等,我们直接使用Opencv的findCont
- 一、死锁简单来说,死锁是一个资源被多次调用,而多次调用方都未能释放该资源就会造成死锁,这里结合例子说明下两种常见的死锁情况。1、迭代死锁该情
- 一、步骤要从 JPEG 图像中获取经纬度信息,可以使用 Python 的 PIL(Python Imaging Library)库。以下是一
- 在本文中,作者介绍了 5 种方法,也许在入门阶段时,我们还不太了解它们,但在实战中这 5 个技巧非常实用。字符串运算字符串本质上也是一种元组
- 这篇文章主要介绍了python使用rsa非对称加密过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要
- JavaScript中有很多内部属性和方法,在大多数情况下,只有JavaScript引擎才可以访问,但不论什么都是有特例的,在这里就是指Mo
- 一、MHA介绍(一)、什么是MHAMHA(MasterHigh Availability)是一套优秀的MySQL高可用环境下故障切换和主从复
- 使用索引获取列表的元素(随机读取)列表元素支持用索引访问,正向索引从0开始colors=["red","blu
- 在训练模型时会在前面加上:model.train()在测试模型时在前面使用:model.eval()同时发现,如果不写这两个程序也可以运行,
- 前言最近在用yolov5参加比赛,yolov5的技巧很多,仅仅用来参加比赛,着实有点浪费,所以有必要好好学习一番,在认真学习之前,首先向yo
- 计算年、月、日需要安装组件包pip install python-dateutil当前日期时间import datetimeprint da
- 第一步:下载和安装python-3.4.4amd.msi可以去官方网站下载,也可以从网盘下载: 链接: https://pan.baidu.
- 在生产环境上,一般会使用比较健壮的Web服务器,如Apache来运行我们的应用。如果我们的Web应用是采用Python开发,而且符合WSGI
- 本文主要介绍了pandas导出数据到文件的四种方式,分享给大家,主要也是给自己留个笔记,具体如下:import pandas as pdim
- python启用多线程后,调用exit出现无法退出的情况,原因是exit会抛出Systemexit的异常,如果在exit外围调用了try,就
- BLOG阅读:http://www.planabc.net/article.asp?id=118在使用CSS实现表现的时候,会经常接触到di
- 计算分页,嘿嘿一次搞定不用判断intNumPage = Abs(Int(-(intNumRecord/intPerPage)))  
- 假设页面源代码如下:<input type="text"name="wd" id="
- 本文介绍了webpack编译vue项目生成的代码探索,分享给大家,具体如下:前言往 main.js 里写入最简单的 vue 项目结构如下im