Question

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

Était-ce utile?

La solution

To make the code behave similar on both Windows and Linux, pass b explicitly:

Process(target=fb2, args=(k, b))

The difference is that on Linux fork is used by default and it copies whatever state the parent process had to the child process. That is why the changes made inside fmt() are visible in children.

Windows uses spawn start method by default that reproduces the global state only partially e.g., values that are set during the import time are visible but changes made inside fmt() are not.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top