Multiprocessing Start Methods
You can change the method used for starting child processes via the multiprocessing.set_start_method() function.
In this tutorial you will discover how to retrieve supported and current start methods and how to change the start method in your Python program.
Need to Change Start Method
A process is a running instance of a computer program.
Every Python program is executed in a Process, which is a new instance of the Python interpreter. This process has the name MainProcess and has one thread used to execute the program instructions called the MainThread. Both processes and threads are created and managed by the underlying operating system.
Sometimes we may need to create new child processes in our program in order to execute code concurrently.
Python provides the ability to create and manage new processes via the multiprocessing.Process class.
In multiprocessing programming, we may need to change the technique used to start child processes.
This is called the start method.
What is a start method and how can we configure it in Python?
Run your loops using all CPUs, download my FREE book to learn how.
What is a Start Method
A start method is the technique used to start child processes in Python.
There are three start methods, they are:
- spawn: start a new Python process.
- fork: copy a Python process from an existing process.
- forkserver: new process from which future forked processes will be copied.
Default Start Methods
Each platform has a default start method.
The following lists the major platforms and the default start methods.
Supported Start Methods
Not all platforms support all start methods.
The following lists the major platforms and the start methods that are supported.
Generally, a fork is considered not safe on macOS.
You can learn more about this here:
Confused by the multiprocessing module API?
Download my FREE PDF cheat sheet
How to Change The Start Method
The multiprocessing package provides functions for getting and setting the start method for creating child processes.
The start method API includes the following functions:
- multiprocessing.get_all_start_methods()
- multiprocessing.get_start_method()
- multiprocessing.set_start_method()
- multiprocessing.get_context()
Let’s take a closer look at each in turn.
How to Get Supported Start Methods
The function returns a list of string values, each representing a supported start method.