This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author frobnitzem
Recipients frobnitzem
Date 2012-05-01.18:56:29
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1335898590.74.0.306368822304.issue14704@psf.upfronthosting.co.za>
In-reply-to
Content
Python Devs,

  There is an issue relating to variable lookup using exec from within multiprocessing's fork()-ed process.  I'm attempting to use the forked process as a generic remote python shell, but exec is unable to reach variables from within functions.  This issue makes it impossible to define a function which uses un-passed variables defined in the remote process.

  The simplest way to reproduce the error is:

--- err.py ---
from multiprocessing import Process, Pipe

def run_remote(con, name):
  my_name = name
  for i in range(2):
    code = con.recv()
    exec code

me, he = Pipe()
p = Process(target=run_remote,
            args=(he, "Sono Inglese de Gerrards Cross."))
p.start()

me.send("print my_name") # works

me.send("""
def show_name():
  print my_name
show_name() # doesn't work
""")
--- end err.py ---

This program prints:
$ python2.6 err.py
Sono Inglese de Gerrards Cross.
Process Process-1:
Traceback (most recent call last):
  File "/sw/lib/python2.6/multiprocessing/process.py", line 232, in _bootstrap
    self.run()
  File "/sw/lib/python2.6/multiprocessing/process.py", line 88, in run
    self._target(*self._args, **self._kwargs)
  File "err.py", line 7, in run_remote
    exec code
  File "<string>", line 4, in <module>
  File "<string>", line 3, in show_name
NameError: global name 'my_name' is not defined

I'm using Mac OSX (10.6.8) and
Python 2.6.5 (r265:79063, Sep 23 2010, 14:05:02) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin

The issue (with the same traceback) also occurs for:
Python 2.7 (r27:82500, Sep 29 2010, 15:34:46) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin


Using exactly the same set of exec calls locally results in the correct behavior.

--- noerr.py ---
my_name = "Sono Inglese de Gerrards Cross."
exec "print my_name"

exec """
def show_name():
  print my_name
show_name()
"""
--- end noerr.py ---
History
Date User Action Args
2012-05-01 18:56:30frobnitzemsetrecipients: + frobnitzem
2012-05-01 18:56:30frobnitzemsetmessageid: <1335898590.74.0.306368822304.issue14704@psf.upfronthosting.co.za>
2012-05-01 18:56:30frobnitzemlinkissue14704 messages
2012-05-01 18:56:29frobnitzemcreate