可能重复:
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模板网!
Python 多处理模块的 .join() 方法到底在做什么?What exactly is Python multiprocessing Module#39;s .join() Method Doing?(Python 多处理模块的 .join() 方法到底在做什么?)
在 Python 中将多个参数传递给 pool.map() 函数Passing multiple parameters to pool.map() function in Python(在 Python 中将多个参数传递给 pool.map() 函数)
multiprocessing.pool.MaybeEncodingError: 'TypeError("multiprocessing.pool.MaybeEncodingError: #39;TypeError(quot;cannot serialize #39;_io.BufferedReader#39; objectquot;,)#39;(multiprocessing.pool.MaybeEnc
Python 多进程池.当其中一个工作进程确定不再需要Python Multiprocess Pool. How to exit the script when one of the worker process determines no more work needs to be done?(Python 多进程池.当其中一
如何将队列引用传递给 pool.map_async() 管理的函数How do you pass a Queue reference to a function managed by pool.map_async()?(如何将队列引用传递给 pool.map_async() 管理的函数?)
与多处理错误的另一个混淆,“模块"对象没yet another confusion with multiprocessing error, #39;module#39; object has no attribute #39;f#39;(与多处理错误的另一个混淆,“模块对象