Multiprocessing Start Methods(转)

原文:https://superfastpython.com/multiprocessing-start-method/

作者:JASON BROWNLEE

Need to Change Start Method

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.

  • Windows (win32): spawn
  • macOS (darwin): spawn
  • Linux (unix): fork

Supported Start Methods

Not all platforms support all start methods.

The following lists the major platforms and the start methods that are supported.

  • Windows (win32): spawn
  • macOS (darwin): spawn, fork, forkserver.
  • Linux (unix): spawn, fork, forkserver.

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 list of supported start methods can be retrieved via the multiprocessing.get_all_start_methods() function.

The function returns a list of string values, each representing a supported start method.

For example:

How to Get The Current Start Method

The current start method can be retrieved via the multiprocessing.get_start_method() function.

The function returns a string value for the currently configured start method.

For example:

How to Set Start Method

The start method can be set via the multiprocessing.set_start_method() function.

The function takes a string argument indicating the start method to use.

This must be one of the methods returned from the multiprocessing.get_all_start_methods() for your platform.

For example:

It is a best practice, and required on most platforms that the start method be set first, prior to any other code, and to be done so within a if __name__ == ‘__main__’ check called a protected entry point or top-level code environment.

For example:

If the start method is not set within a protected entry point, it is possible to get a RuntimeError such as:

It is also a good practice and required on some platforms that the start method only be set once.

Best Practices For Setting the Start Method

In summary, the rules for setting the start method are as follows:

  1. Set the start method first prior to all other code.
  2. Set the start method only once in a program.
  3. Set the start method within a protected entry point.

How to Set Start Method Via Context

A multiprocessing context configured with a given start method can be retrieved via the multiprocessing.get_context() function.

This function takes the name of the start method as an argument, then returns a multiprocessing context that can be used to create new child processes.

For example:

The context can then be used to create a child process, for example:

It may also be possible to force the start method.

This can be achieved via the “force” argument provided on the set_start_method() implementation in the DefaultContext, although not documented.

For example:

Now that we know how to configure the start method, let’s look at some worked examples.


Free Python Multiprocessing Course

Download my multiprocessing API cheat sheet and as a bonus you will get FREE access to my 7-day email course.

Discover how to use the Python multiprocessing module including how to create and start child processes and how to use a mutex locks and semaphores.

Learn more
 


Example of Getting Supported Start Methods

We can get a list of all supported start methods for the current platform.

This can be achieved via the multiprocessing.get_all_start_methods() function.

The example below gets the list of supported start methods for the current platform, then reports the result.

Running the example gets the list of supported start methods for the current platform.

In this case, we can see the current platform supports all three start methods.

Your specific results may differ, depending on your platform.

Overwheled by the python concurrency APIs?
Find relief, download my FREE Python Concurrency Mind Maps

Example of Getting the Current Start Method

We can get the name of the currently configured start method.

This can be achieved via the multiprocessing.get_start_method() function

The example below gets the currently configured start method and reports it.

Running the example retrieves the currently configured start method.

In this case, the ‘spawn‘ method is the default start method.

Your specific result may differ, depending on your platform.

Example of Setting the Start Method

We can configure the start method using the multiprocessing.set_start_method() function.

In this section we can explore examples of setting each of the different types of start methods and then using it to start a new child process.

Specifically, we will look at starting a child process using the spawn, fork, and forkserver start methods.

Let’s take a closer look at each.

Example of Spawn Start Method

We can start a child process using the ‘spawn‘ start method.

Once configured, we can confirm that the start method was changed.

We can then create a new child process using the start method to execute a custom function.

Tying this together, the complete example is listed below.

Running the example first configures the start method as ‘spawn‘.

The current start method is retrieved and reported, confirming that the change has taken effect.

A new child process is then created using the start method.

Example of Fork Start Method

We can start a child process using the ‘fork‘ start method.

Once configured, we can confirm that the start method was changed.

We can then create a new child process using the start method to execute a custom function.

Tying this together, the complete example is listed below.

Running the example first configures the start method as ‘fork‘.

The current start method is retrieved and reported, confirming that the change has taken effect.

A new child process is then created using the start method.

Example of ForkServer Start Method

We can start a child process using the ‘forkserver‘ start method.

Recall that the forkserver method will start a new server process and use a copy of this process each time a child process is created.

Once configured, we can confirm that the start method was changed.

We can then create a new child process using the start method to execute a custom function.

Tying this together, the complete example is listed below.

Running the example first configures the start method as ‘forkserver‘.

The current start method is retrieved and reported, confirming that the change has taken effect.

A new child process is then created using the start method.

Example of Multiple Start Methods

We can use multiple start methods within our program via a multiprocessing context.

In this example we will set the baseline start method to ‘fork‘ and use it to start a new child process. We will then create a new multiprocessing context with the ‘spawn’ start method, then use it to start a child process.

First, we can set the baseline start method to ‘fork‘, confirm it has been set, then start a new process using the method.

Next, we can create a new multiprocessing context using the ‘spawn‘ start method.

We can then confirm that the context uses the configured start method.

Finally, we can create a new child process using the context with the different start method.

Tying this together, the complete example is listed below.

Running the example first sets the baseline start method to ‘fork‘.

It then confirms that the start method was changed as expected and then creates and starts a new child process using the start method.

Next, a new multiprocessing context is created with a different start method, ‘spawn‘ in this case.

The start method of the new context is then confirmed and a new child process is created using the start method in the created context, different from the baseline start method.

Example of RuntimeError Setting Start Method

We may get a RuntimeError when setting the start method.

This can happen when setting a specific start method, e.g. ‘spawn‘ outside of a protected entry point.

We can demonstrate this with a worked example.

The example below attempts to start the start method to ‘spawn‘ first, directly after the import statements and outside of the protected entry point.

The complete example is listed below.

Running the example results in a RuntimeError indicating that the start method has already been set.

Interestingly, we don’t get the error if we attempt to set the start method to ‘fork‘ outside of the protected entry point.

The fix involves moving the call to set_start_method() to be the first line within the protected entry point.

For example:

 

Further Reading

 

This section provides additional resources that you may find helpful.

Books

I would also recommend specific chapters in the books:

 

Guides

APIs

 

 

 

 

Takeaways

You now know how to configure the start method for processes in Python.

Do you have any questions?
Ask your questions in the comments below and I will do my best to answer.

Photo by Greg Rosenke on Unsplash

 

posted @ 2023-04-21 09:51  奋斗终生  Views(69)  Comments(0Edit  收藏  举报