Start methods#
You can start processes using different methods, which generally determine what data the process shares with the parent process. This page provides an overview of the practical aspects of working with different startup methods.
import multiprocessing
Loading modules#
One practical aspect of using different start methods is that the spawn
and forkserver
methods reload the modules required to run the process, whereas the fork
method uses the imports from the parent process.
The following cell starts a module that prints “module is loaded” when it starts, and defines the variable a = "original variable"
.
%%writefile start_methods_files/value.py
print("module is loaded")
a = "original value"
def p_val(): print(a)
Overwriting start_methods_files/value.py
The following cell imports the module into the main process runtime, and changes the value of the a
variable.
from start_methods_files import value
value.a = "new value"
module is loaded
The next cells executes p_val
function using different start methods.
def run_with_context(method):
context = multiprocessing.get_context(method)
p = context.Process(target=value.p_val)
p.start()
p.join()
run_with_context("spawn")
module is loaded
original value
run_with_context('fork')
new value
run_with_context('forkserver')
module is loaded
original value
As the result “spawn” and “forkserver” methods caused “module is loaded” message, and a
value in them corresponds to the one defined in the module. At the same time, “fork” method doesn’t print “module is loaded” message because it was already loaded by parent process, and prints “new value” because it is the value of the a
defined in the main process.