我有
import concurrent.futures
from multiprocessing import Process,Manager,freeze_support
from functools import partial
def setup():
freeze_support()
global manager
manager=Manager()
global dict1
dict1=manager.dict()
global dict2
dict2=manager.dict()
load_stuff()
以后:
def foo(file):
#do some stuff
foobar()
def foobar():
#look up some stuff in dict1,dict2
def load_stuff():
f=partial(foo,dict1,dict2)
with concurrent.futures.ProcessPoolExecutor() as executor:
for users, tweets in executor.map(f, list_of_files):
但我一直在
NameError: global name 'dict1' is not defined
基本上:我正在产生多个进程,他们在做一些工作时调用一个函数来查找两个dicts中的值。 dicts是全局的,因为我需要它们,因为我可能只是调用foo()和foobar()而不是在新进程中。进程无法访问dicts。我该如何解决这个问题?
编辑:我最终将dict1作为普通的全局字典,然后将其作为变量传递给f,然后将其重新声明为f中的全局字典并且它可以工作。不是内存效率高,但这个过程只运行一次,而dicts只使用45mb左右。
答案 0 :(得分:2)
由于默认参数值在编译时被绑定,因此应该起作用:
def foobar(dict1=dict1, dict2=dict2):
pass
等等,因此需要在load_stuff
之前定义setup
,setup
之前需要定义foobar
,但似乎foobar
和{ {1}}彼此相邻。
我不太清楚你的功能是如何布局的。也许你需要在某个地方另外声明load_stuff
和dict1
作为模块级变量(同时保持全局语句不变)。
那就是说,我相信你在并发程序中访问共享变量的方法不是惯用的。