[issue20775] Modifications to global variables ignored after instantiating multiprocessing.Pool

Tim Peters report at bugs.python.org
Wed Feb 26 05:30:36 CET 2014


Tim Peters added the comment:

This is expected.  "global" has only to do with the visibility of a name within a module; it has nothing to do with visibility of mutations across processes.  On a Linux-y system, executing Pool(3) creates 3 child processes, each of which sees a read-only *copy* of the state of the module at the time (the usual copy-on-write fork() semantics).  From that point on, nothing done in the main program can have any effect on the data values seen by the child processes, nor can anything done by a child process have any effect on the data values seen by the main program or by the other child processes, unless such data values are _explicitly_ shared via one of the cross-process data sharing mechanisms the multiprocessing module supports.

So, in your program, all child processes see name == "Not Updated", because that's the value `name` had at the time the processes were created.  The later

    name = "Updated"

changes the binding in the main program, and only in the main program.  If you want child processes to see the new value you should, e.g., pass `name` to f().

----------
nosy: +tim.peters

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue20775>
_______________________________________


More information about the Python-bugs-list mailing list