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
Multiprocessing infinite loop #52341
Comments
Hi, The following code results in an infinite loop --> # ---- import multiprocessing
def f(m):
print(m)
p = multiprocessing.Process(target=f, args=('pouet',))
p.start()
# I've firstly think about an issue in my code when Python loads this module so I've added a "if __name__ == '__main__':" and it works well. But, with the following code Python enters in the same infinite loop --> proc.py: # ---- import multiprocessing
def f(m):
print(m)
class P:
def __init__(self, msg):
self.msg = msg
def start(self):
p = multiprocessing.Process(target=f, args=(self.msg,))
p.start()
# my_script.py: from proc import P
p = P("pouet")
p.start()
# It's like Python loads all parent's process memory space before starting process which issues in an infinite call to Process.start() ... |
Are you running this on windows? |
Sorry I've not made clear my working platform. Yes, I'm running Python 3.1.1 32 bit on a Windows 7 x64. |
The restriction that imposes the "__name__= '__main__'" idiom also applies when multiprocessing is not used in the main module. Actually the main module is always reloaded in the subprocess. The docs should be more explicit about it. |
I have tried to clearly state that the main module is imported by a newly created process. I have also added a comment explaining that an infinite loop like the one Benjamin describes could be created. |
I think I have a similar situation: C:\Py\Scripts\foo if __name__ == '__main__':
import bar
bar.main() C:\Py\Lib\site-packages\bar.py from multiprocessing import Pool
def task(arg):
return arg
def main():
pool = Pool()
res = pool.apply_async(task, (3.14,))
if __name__ == '__main__':
main() I can run "python bar.py". "python C:\Py\Scripts\foo" yields an infinite stream of errors: File "<string>", line 1 in <module> This same scheme works fine on linux. Have I just overlooked something simple? |
Your code doesn't appear to have anything to do with the reported bug, which is about an infinite loop. But FYI to my understanding your script can't work on windows, since foo can't be imported. On linux, foo doesn't need to be imported. |
As an alternative, see http://bugs.python.org/issue10845 It contains a patch for the 3.X series which fixes the infinity loop. Applying it to the 2.X series will fix the issue and make a change the documentation unnecessary. |
Actually sorry, now that I reread the details a second time, I'm not sure that is this the same deal. I'll just file a separate bug. |
On Windows 7 (64-bit), it was not possible to reproduce any infinite looping behavior with the supplied example code. Specifically, with the two examples from Benjamin, the observed behavior when running them was the same under 2.7.8 and default (3.5): a RuntimeError results with a message suggesting that the code perhaps needs to leverage the "if __name__ == '__main__'" idiom to avoid having both parent and all subsequent children processes starting up a new process because they're all unintentionally running the same lines of code intended only for the parent to run. Adding that idiomatic test to each of the two examples permits them to run to a happy conclusion. That is, in the case of the first example we make that one-line code change to read: import multiprocessing
def f(m):
print(m)
if __name__ == '__main__':
p = multiprocessing.Process(target=f, args=('pouet',))
p.start()
# This would be a recommended practice on unix-y systems as well as Windows. Aside: It was not possible to reproduce the issue injected by Darren either -- perhaps the example code provided was not quite what he intended. The infinite looping behavior described in the original issue description might well have been reproducible in much earlier releases. In the current default (3.5) branch (or in 2.7.8), it is no longer possible to reproduce. I'm tempted to mark this as "out of date" but instead will opt for "works for me" and close the issue. |
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: