在多处理python中无法访问全局变量
我的应用程序需要执行一些任意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
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
函数时访问它。