Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this multiprocessing code fail? [duplicate]

Tags:

python

def sample():
    pass

Process(target=sample).start()
Process(target=sample).start()

The above code fails with an error:

An attempt has been made to start a new process before the current process has finished its bootstrapping phase. This probably means that you are not using fork to start your child processes and you have forgotten to use the proper idiom in the main module

But this code runs fine:

def sample():
    pass
if __name__ == '__main__':
    Process(target=sample).start()
    Process(target=sample).start()

Why does the main module affect my code execution in this case? I am unable to understand the error message properly.

Note: I went through What does if __name__ == "__main__": do? but could not understand its relevance to my code.

like image 418
codingsplash Avatar asked Dec 11 '15 12:12

codingsplash


People also ask

What is deadlock in multiprocessing?

A deadlock is a condition that may happen in a system composed of multiple processes that can access shared resources. A deadlock is said to occur when two or more processes are waiting for each other to release a resource. None of the processes can make any progress.

How do you cancel multiprocess?

We can kill or terminate a process immediately by using the terminate() method. We will use this method to terminate the child process, which has been created with the help of function, immediately before completing its execution.

What is multiprocessing dummy?

dummy module module provides a wrapper for the multiprocessing module, except implemented using thread-based concurrency. It provides a drop-in replacement for multiprocessing, allowing a program that uses the multiprocessing API to switch to threads with a single change to import statements.


1 Answers

When you create a new child process, the child process might (mostly depending on the OS you are using) re-import the current module.

In your case, re-importing the module also executes these two lines:

Process(target=sample).start()
Process(target=sample).start()

and what happens is, what the error message tells you:

An attempt has been made to start a new process before the current process has finished its bootstrapping phase. This probably means that you are not using fork to start your child processes and you have forgotten to use the proper idiom in the main module

While setting up the proper environment for your first child process, the code tries to fork off another child process. The manager detects this and tells you that this is not okay.

if __name__ == '__main__':
    Process(target=sample).start()
    Process(target=sample).start()

is a guard condition that allows the current module to be imported in the child module without this problem, because only the --well-- main module's name is __main__.

like image 151
dhke Avatar answered Oct 31 '22 00:10

dhke