简体   繁体   English

使用多处理时,全局变量未在python中更新

[英]Global variable not updated in python while using multiprocessing

I have a following simple code 我有一个简单的代码

from multiprocessing import Pool
x = []


def func(a):
    print(x,a)


def main():
    a = [1,2,3,4,5]
    pool = Pool(1)
    global x
    x = [1,2,3,4]
    ans = pool.map(func,a)
    print(x)

It gives me the result 它给了我结果

[] 1
[] 2
[] 3
[] 4
[] 5
[1, 2, 3, 4]

I expected the result to reflects the change in global variable x. 我期望结果反映全局变量x的变化。 Which seems that the changed in global variable x is not updated before the pool call. 似乎全局变量x中的更改在池调用之前未更新。 I would like to ask what is the cause of this? 我想问一下这是什么原因?

So I have done what GuangshengZuo suggested, and sadly the result was not desirable. 所以我做了广生作的建议,遗憾的是结果不可取。 After looking deeper into it, I realized the problem was not because of script, but rather the OS. 在深入研究之后,我意识到问题不是因为脚本,而是因为操作系统。

In windows, there is no os.fork() , hence the change in global variable is not copied. 在Windows中,没有os.fork() ,因此不会复制全局变量的更改。 But, on Unix machine, the script works fine. 但是,在Unix机器上,脚本运行正常。

Two seperate processes will not share the same global variables. 两个单独的进程不会共享相同的全局变量。 A multiprocessing pool abstracts away the fact that you are using two seperate processes which makes this tough to recognise. 多处理池抽象出你正在使用两个单独的进程这一事实,这使得这很难识别。

I think it is because this is multiprocess, not multithread. 我认为这是因为这是多进程,而不是多线程。 the main process and the new process does not share a same global variable. 主进程和新进程不共享相同的全局变量。 So the new process has the copy of the main process when x is [], and after created, main process change x's value, but it does not change to new process's x. 因此,当x为[]时,新进程具有主进程的副本,并且在创建之后,主进程更改x的值,但它不会更改为新进程的x。

if change the code to this : 如果将代码更改为:

from multiprocessing import Pool
x = []


def func(a):
    print(x,a)


def main():
    a = [1,2,3,4,5]
    global x
    x = [1,2,3,4]
    pool = Pool(1)
    ans = pool.map(func,a)
    print(x)

and the ouput will be what you want. 而输出将是你想要的。 Notice the pool = Pool(1) 's position 注意pool = Pool(1)的位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM