[issue20775] Modifications to global variables ignored after instantiating multiprocessing.Pool
New submission from Naftali Harris: Hi everyone, It appears that if you use a global variable in a function that you pass to Pool.map, but modify that global variable after instantiating the Pool, then the modification will not be reflected when Pool.map calls that function. Here's a short script, (also attached), that demonstrates what I mean: $ cat reproduces.py from multiprocessing import Pool name = Not Updated def f(ignored): print(name) def main(): global name p = Pool(3) name = Updated p.map(f, range(3)) if __name__ == __main__: main() $ python reproduces.py Not Updated Not Updated Not Updated If the `name = Updated' line is moved above the `p = Pool(3)' line, then the script will print Updated three times instead. This behavior is present in versions 2.6, 2.7, 3.1, 3.2, 3.3, and 3.4. I run Linux Mint 14 (nadia), on an Intel i5-3210M processor (four cores). Is this expected behavior? Thanks very much, Naftali -- components: Library (Lib) files: reproduces.py messages: 212221 nosy: Naftali.Harris priority: normal severity: normal status: open title: Modifications to global variables ignored after instantiating multiprocessing.Pool type: behavior versions: Python 2.7, Python 3.1, Python 3.2, Python 3.3, Python 3.4 Added file: http://bugs.python.org/file34223/reproduces.py ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue20775 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue20775] Modifications to global variables ignored after instantiating multiprocessing.Pool
Changes by Ned Deily n...@acm.org: -- nosy: +sbt ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue20775 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue20775] Modifications to global variables ignored after instantiating multiprocessing.Pool
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 rep...@bugs.python.org http://bugs.python.org/issue20775 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue20775] Modifications to global variables ignored after instantiating multiprocessing.Pool
Naftali Harris added the comment: Oh, ok, that makes a lot of sense. Thanks for the clear and patient explanation, Tim! Sorry to have bothered the Python bug tracker with this. --Naftali -- resolution: - invalid status: open - closed ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue20775 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com