Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Python's multiprocessing module import __main__ when starting a new process on Windows?

I am playing around with a library for my beginner students, and I'm using the multiprocessing module in Python. I ran into this problem: importing and using a module that uses multiprocessing without causing infinite loop on Windows

As an example, suppose I have a module mylibrary.py:

# mylibrary.py  from multiprocessing import Process  class MyProcess(Process):     def run(self):         print "Hello from the new process"  def foo():     p = MyProcess()     p.start() 

And a main program that calls this library:

# main.py  import mylibrary  mylibrary.foo() 

If I run main.py on Windows, it tries to import main.py into the new process, meaning the code is executed again which results in an infinite loop of process generation. I can fix it like so:

import mylibrary  if __name__ == "__main__":     mylibrary.foo() 

But, this is pretty confusing for beginners, and moreover it seems like it shouldn't be necessary. The new process is being created in mylibrary, so why doesn't the new process just import mylibrary? Is there a way to work around this issue without having to change main.py?

I am using Python 2.7, by the way.

like image 979
Laura Avatar asked Jan 05 '13 19:01

Laura


People also ask

Does Python multiprocessing work on Windows?

The multiprocessing package offers both local and remote concurrency, effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads. Due to this, the multiprocessing module allows the programmer to fully leverage multiple processors on a given machine. It runs on both Unix and Windows.

How does multiprocessing process work in Python?

The multiprocessing package supports spawning processes. It refers to a function that loads and executes a new child processes. For the child to terminate or to continue executing concurrent computing,then the current process hasto wait using an API, which is similar to threading module.

How do you start a new process in Python?

To start a new process, or in other words, a new subprocess in Python, you need to use the Popen function call. It is possible to pass two parameters in the function call. The first parameter is the program you want to start, and the second is the file argument.

How do I stop multiprocessing in Python?

A process can be killed by calling the Process. kill() function. The call will only terminate the target process, not child processes. The method is called on the multiprocessing.


1 Answers

Windows doesn't have fork, so there's no way to make a new process just like the existing one. So the child process has to run your code again, but now you need a way to distinguish between the parent process and the child process, and __main__ is it.

This is covered in the docs here: http://docs.python.org/2/library/multiprocessing.html#windows

I don't know of another way to structure the code to avoid the fork bomb effect.

like image 193
Ned Batchelder Avatar answered Oct 11 '22 12:10

Ned Batchelder