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 th开发者_运维知识库e 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 {}
the problem is clearly described in the documentation for the multiprocessing module: windows lacks a fork()
system call, so on windows, the module source code gets reevaluated at the start of each process, thus the current state is not preserved. (on un*x based system, including OSX, Process is implemented using a fork, and the state is preserved)
note that i am talking about the state of the whole python module.
your way of holding a state is wrong: the a
and b
members of the State
class are class-wide 'static' members, they are not part of an instance of a State
object. when doing state.a = 11
, you are modifying the State
class, not an instance of the class.
your code would have worked fine if a
and b
had been instance members of the State
class:
class State(object):
def __init__(self)
self.a = 0
self.b = {}
then, passing the state
instance to a new Process, would have correctly passed the value as expected.
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))
精彩评论