路过的Python中的字典多处理 [英] Python passing dictionary to process in multiprocessing
问题描述
我有一个包含不同的属性,包括少数词典的(大)数量级。当我通过对一个新的进程传递类的一个实例,所有的数值似乎正确地获得通过,但在班上对象中的任何字典得到清空。
下面是一个简单的测试,我做了演示我的问题:
从多进口流程级别状态:
a = 0时
B = {}DEF F(S,I):
打印F:,S.A,S.B高清的main(): 状态=状态() 状态。a = 11
state.b ['测试'] = 12 打印主:状态。a,state.b PS = []
因为我在范围(1):
P =过程(目标= F,ARGS =(州,I))
p.start()#做的工作
ps.append(p)的
在PS号码:
p.join()如果__name__ =='__main__':
主要()
我希望可以将输出为
主营:11 {'测试':12}
F:11 {'测试':12}
而是我得到
主营:11 {'测试':12}
F:11 {}
我最终通过传递词典中明确除国家解决这个问题的工作。例如。
P =过程(目标= F,ARGS =(州,我,state.b))
I have a class that contains a (large) number of different properties, including a few dictionaries. When I pass an instance of the class through to a new process, all of the numeric values seem to get passed in correctly, but any dictionaries that were in the class object get emptied.
Here's a simple test I cooked up that demonstrates my problem:
from multiprocessing import Process
class State:
a = 0
b = {}
def f(s, i):
print "f:", s.a, s.b
def main():
state = State()
state.a = 11
state.b['testing'] = 12
print "Main:", state.a, state.b
ps = []
for i in range(1):
p = Process(target=f, args=(state, i))
p.start() # Do the work
ps.append(p)
for p in ps:
p.join()
if __name__ == '__main__':
main()
I expect the output to be
Main: 11 {'testing': 12}
f: 11 {'testing': 12}
but instead I get
Main: 11 {'testing': 12}
f: 11 {}
I ended up working around this problem by passing the dictionaries in explicitly in addition to the state. e.g.
p = Process(target=f, args=(state, i, state.b))
这篇关于路过的Python中的字典多处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!