在多处理python中无法访问全局变量

在多处理python中无法访问全局变量,python,multiprocessing,Python,Multiprocessing,我的应用程序需要执行一些任意python代码作为输入。 当我尝试使用多处理模块在不同的进程中执行它时,代码的globals()存在一些问题 考虑下面的代码,它按预期工作 src = """import os def x(): print(os.getcwd())""" exec (src) eval('x()') 此代码按预期工作,没有任何错误 当我想用下面的代码执行多处理时,它会给我一个错误,它找不到os from concurrent.futures import

我的应用程序需要执行一些任意python代码作为输入。 当我尝试使用多处理模块在不同的进程中执行它时,代码的
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

未定义名称“os”

由给出的答案为我解决了这个问题,但有人能解释一下,在
多处理的情况下,为什么需要传递
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)
更新:
我认为您的报告
名称“os”未定义的原因是
os
导入在函数
x
中不知何故超出了范围(如果我将
导入os
移动到
x
函数中,您的代码将实际工作),因此我决定添加
全局()
作为参数,以便
exec()
可以将
os
导入
globals()
并在
eval()调用
x
函数时访问它

如果在
exec(source,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>
==========
{'my_eval':,'ProcessPoolExecutor':,'src':'import os\ndef x():\n print(os.getcwd()),'uuu内置文件:,'uu文件:'test.py','uu包:'uuuuuuuuuuu','uuuuuuuuuuu名称:'uuuuuuuuuuuu主文件','uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
==========
++++++++++
{'my_eval':,'ProcessPoolExecutor':,'src':'import os\ndef x():\n print(os.getcwd()),'uuuu builtins\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
++++++++++

它可以工作,但您能解释一下,在多处理中需要额外的东西吗?我想您的报告
名称“os”没有定义的原因是
os
导入在函数
x
中不知何故超出了范围(如果我将
import os
移动到
x
函数中,您的代码实际上会工作),因此我决定添加
globals()
作为参数,以便
exec()
可以将
os
导入到
globals()
中,并在
eval()
调用
x
函数时访问它。