Python 多处理和全局管理器

Python 多处理和全局管理器,python,dictionary,process,multiprocessing,future,Python,Dictionary,Process,Multiprocessing,Future,我有 后来: 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 di

我有

后来:

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):
基本上:我正在生成多个进程,这些进程在执行某些工作时调用一个函数,该函数在两个dict中查找值。dicts是全局的,因为当我可能只调用foo()和foobar()而不在新进程中时,我需要它们。进程无法访问DICT。我怎样才能解决这个问题


编辑:我最终使dict1成为一个普通的全局字典,然后将它作为一个变量传递给f,然后在f内部将它重新定义为全局字典,它就工作了。内存效率不高,但此过程只运行一次,DICT仅使用45mb左右。

由于默认参数值是在编译时绑定的,因此应该可以:

 NameError: global name 'dict1' is not defined

等等,所以
load\u stuff
需要在
setup
之前定义,而
setup
需要在
foobar
之前定义,但似乎
foobar
load\u stuff
是相邻的

我不太清楚你的职能是如何安排的。也许您需要在某个地方额外声明
dict1
dict2
作为模块级变量(同时保持全局语句不变)



也就是说,我相信您在并发程序中访问共享变量的方法不是惯用方法。

我最终只是将其作为参数传递给f,然后将其声明为全局变量。我知道它的内存效率不高,但dict在内存中只有45mb,这很好。此过程无论如何只运行一次。
def foobar(dict1=dict1, dict2=dict2):
    pass