3
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

7
  • 1
    Why are you using global variables in the first place? Don't be that guy. Commented Mar 12, 2014 at 7:33
  • Looks like on Linux you printed b[k] (despite your code) while on Windows you really printed b (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. Commented Mar 12, 2014 at 8:08
  • To test on Linux, you could use Python 3.4 and multiprocessing.set_start_method('spawn') to avoid fork-semantics. Commented Mar 12, 2014 at 15:15
  • global b is useless unless there is b = .. in the function (or other binding statement) Commented Mar 12, 2014 at 15:23
  • The first print is my mistake; Commented Mar 20, 2014 at 2:51

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.