繁体   English   中英

全局变量在Multiprocessing python中不可访问

[英]globals are not accessible inside the Multiprocessing python

我的应用程序需要执行一些任意的python代码作为输入。 当我尝试使用Multiprocessing模块在不同的过程中执行它时,代码的globals()出现了一些问题。

考虑下面的代码可以正常工作。

src = """import os
    def x():
        print(os.getcwd())"""

exec (src)
eval('x()')

此代码可以正常工作,没有任何错误。

当我想使用以下代码通过多处理执行同一件事时,它给了我找不到os的错误?

from concurrent.futures import ProcessPoolExecutor
pool = ProcessPoolExecutor(max_workers=1)

src = """import os
def x():
    print(os.getcwd())"""

def my_eval(source):
    try:
        exec (source)
        eval('x()')
    except Exception as ex:
        print(ex)

pool.submit(my_eval, src)

在这种情况下的输出是

<Future at 0x7fcc4c160f28 state=running>
name 'os' is not defined

viethtran给出的答案为我解决了这个问题,但是有人可以解释为什么在multiprocessing情况下我们需要传递globals ,而在正常执行的情况下为什么不需要传递globals

当我将globals()作为exec()函数的参数添加时,它起作用。

from concurrent.futures import ProcessPoolExecutor
pool = ProcessPoolExecutor(max_workers=1)

src = """import os
def x():
    print(os.getcwd())"""

def my_eval(source):
    try:
        exec (source, globals())
        eval('x()')
    except Exception as ex:
        print(ex)

pool.submit(my_eval, src)

更新:我发现name 'os' is not defined报告name 'os' is not defined的原因是因为以某种方式将os导入置于函数x的范围之外(如果我将import os移到x函数中,则您的代码将真正起作用)所以我决定添加globals()作为参数,以便exec()可以将os导入添加到globals()并在x函数由eval()调用时进行访问。

如果在exec (source, globals()) globals()之前和之后打印出globals() ,您会注意到模块os和函数x将被添加到globals()字典中。

from concurrent.futures import ProcessPoolExecutor
pool = ProcessPoolExecutor(max_workers=1)

src = """import os
def x():
    print(os.getcwd())"""

def my_eval(source):
    try:
        print('==========')
        print(globals())
        print('==========')
        exec(source, globals())
        print('++++++++++')
        print(globals())
        print('++++++++++')
        eval('x()')
    except Exception as ex:
        print(ex)

pool.submit(my_eval, src)

此代码将打印出如下内容:

==========
{'my_eval': <function my_eval at 0x7ff63c7baaa0>, 'ProcessPoolExecutor': <class 'concurrent.futures.process.ProcessPoolExecutor'>, 'src': 'import os\ndef x():\n    print(os.getcwd())', '__builtins__': <module '__builtin__' (built-in)>, '__file__': 'test.py', '__package__': None, '__name__': '__main__', '__doc__': None, 'pool': <concurrent.futures.process.ProcessPoolExecutor object at 0x7ff640c14050>}
==========
++++++++++
{'my_eval': <function my_eval at 0x7ff63c7baaa0>, 'ProcessPoolExecutor': <class 'concurrent.futures.process.ProcessPoolExecutor'>, 'src': 'import os\ndef x():\n    print(os.getcwd())', '__builtins__': <module '__builtin__' (built-in)>, '__file__': 'test.py', '__package__': None, 'x': <function x at 0x7ff63c7bac80>, '__name__': '__main__', 'os': <module 'os' from '/home/viet/anaconda2/lib/python2.7/os.pyc'>, '__doc__': None, 'pool': <concurrent.futures.process.ProcessPoolExecutor object at 0x7ff640c14050>}
++++++++++
<your pwd here>

暂无
暂无

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

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