Title: NameError Issue in Multiprocessing
Type: Stage:
Components: None Versions: Python 2.6
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: frobnitzem, mark.dickinson
Priority: normal Keywords:

Created on 2012-05-01 18:56 by frobnitzem, last changed 2012-05-01 19:16 by mark.dickinson. This issue is now closed.

Messages (2)
msg159764 - (view) Author: David M. Rogers (frobnitzem) Date: 2012-05-01 18:56
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:

--- ---
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."))

me.send("print my_name") # works

def show_name():
  print my_name
show_name() # doesn't work
--- end ---

This program prints:
$ python2.6
Sono Inglese de Gerrards Cross.
Process Process-1:
Traceback (most recent call last):
  File "/sw/lib/python2.6/multiprocessing/", line 232, in _bootstrap
  File "/sw/lib/python2.6/multiprocessing/", line 88, in run
    self._target(*self._args, **self._kwargs)
  File "", 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.

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

exec """
def show_name():
  print my_name
--- end ---
msg159766 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2012-05-01 19:16
Thanks for the report.

This is expected behaviour.  It isn't actually anything to do with multiprocessing;  it's to do with invoking exec from within a function scope.  You can see the same effect with code like this:

code = """\
def show_name():
    print my_name

def run():
    my_name = "me"
    exec code



for more explanation.
Date User Action Args
2012-05-01 19:16:35mark.dickinsonsetstatus: open -> closed

nosy: + mark.dickinson
messages: + msg159766

resolution: not a bug
2012-05-01 18:56:30frobnitzemcreate