New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NameError Issue in Multiprocessing #58909
Comments
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() me.send("print my_name") # works me.send(""" 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 The issue (with the same traceback) also occurs for: 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 """ |
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
show_name()
"""
def run():
my_name = "me"
exec code
run() See http://docs.python.org/reference/executionmodel.html#interaction-with-dynamic-features for more explanation. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: