问题描述
可能重复:
Python 多处理全局变量更新未返回给父级
我正在使用具有多个内核的计算机,为了提高性能,我真的应该使用多个内核.但是,我很困惑为什么这些代码没有达到我的预期:
I am using a computer with many cores and for performance benefits I should really use more than one. However, I'm confused why these bits of code don't do what I expect:
from multiprocessing import Process
var = range(5)
def test_func(i):
global var
var[i] += 1
if __name__ == '__main__':
jobs = []
for i in xrange(5):
p = Process(target=test_func,args=(i,))
jobs.append(p)
p.start()
print var
还有
from multiprocessing import Pool
var = range(5)
def test_func(i):
global var
var[i] += 1
if __name__ == '__main__':
p = Pool()
for i in xrange(5):
p.apply_async(test_func,[i])
print var
我希望结果是 [1, 2, 3, 4, 5] 但结果是 [0, 1, 2, 3, 4].
I expect the result to be [1, 2, 3, 4, 5] but the result is [0, 1, 2, 3, 4].
在将全局变量与进程一起使用时,我肯定遗漏了一些微妙之处.这甚至是要走的路还是我应该避免尝试以这种方式更改变量?
There must be some subtlety I'm missing in using global variables with processes. Is this even the way to go or should I avoid trying to change a variable in this manner?
推荐答案
如果您正在运行两个单独的进程,那么它们将不会共享相同的全局变量.如果要在进程之间传递数据,请查看使用 send 和 recv.看看 http://docs.python.org/library/multiprocessing.html#shared-state-between-processes 举一个与您正在做的类似的例子.
If you are running two separate processes, then they won't be sharing the same globals. If you want to pass the data between the processes, look at using send and recv. Take a look at http://docs.python.org/library/multiprocessing.html#sharing-state-between-processes for an example similar to what you're doing.
这篇关于全局变量和 Python 多处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)