import os
from multiprocessing import Process
b = {
'x':{
'd':[]
},
'y':{
'd':[]
},
}
def fmt():
global b
for k in b:
if not b[k]['d']:
b[k]['d'].append("fb1")
print b
t = Process(target=fb2, args=(k,))
t.daemon = False
t.start()
def fb2(k="x"):
print os.getpid(), k, b[k]
if __name__ == '__main__':
fmt()
Windows output:
C:\Python27\python.exe C:/Users/qchen/PycharmProjects/syntax_test/syntax_test.py
{'y': {'d': ['fb1']}, 'x': {'d': []}}
{'y': {'d': ['fb1']}, 'x': {'d': ['fb1']}}
4412 y {'d': []}
5972 x {'d': []}
Linux Output:
qchen@ubuntu:~/PycharmProjects/for_test$ python syntax_test.py
{'y': {'d': ['fb1']}, 'x': {'d': []}}
{'y': {'d': ['fb1']}, 'x': {'d': ['fb1']}}
23547 y {'d': ['fb1']}
23548 x {'d': ['fb1']}
I don't know why it is different between Windows OS and Linux OS; The difference is because of the difference of Process Fork and management in two OS
b[k](despite your code) while on Windows you really printedb(as the code states). Since there is a difference in the used code (at least that's the simplest explanation), you probably messed up sth else. I propose to compare both versions you run on the two systems, remove all differences, and retry that experiment. Probably the peculiarities are gone then.multiprocessing.set_start_method('spawn')to avoidfork-semantics.global bis useless unless there isb = ..in the function (or other binding statement)