Last Updated on September 12, 2022
You can configure the name of a process and whether it is a daemon via the multiprocessing.Process class constructor.
In this tutorial you will discover how to configure a new child process in Python.
Let’s get started.
Need to Configure a Child Process
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.
You can learn more about multiprocessing in the tutorial:
It is possible to assign child processes a unique name and to make them become daemon (background) processes, although this requires some configuration.
How can we configure new child processes?
Run loops using all CPUs, download your FREE book to learn how.
How to Configure a Child Process
We can configure a new multiprocessing.Process via the constructor or via setting attributes before the process has started.
There are two attributes of the process that we can configure, they are:
- Process name: the name used to identify the process.
- Process daemon: whether the process is a background process (daemon) or not.
Setting a unique or meaningful process name is helpful when debugging and logging to differentiate it from other possible processes of execution.
Processes are not daemon processes (background process) by default when created in the main or parent process. New processes take on the daemon value of the parent process. Therefore, because the main parent process is not a daemon process, new processes created in the main parent process too will not be daemon processes.
The parent Python process will run as there are non-daemon child processes (and threads) executing and will stop as soon as all non-daemon processes finish. This means that the daemon process can be used for helpful background tasks that should not stop the program from exiting.
When creating a new instance of a multiprocessing.Process, we can specify the name of the process via the “name” argument that takes a string and whether the process is a daemon process via the “daemon” boolean argument.
For example:
1 2 3 |
... # create a daemon process with a custom name process = multiprocessing.Process(name='MyProcess', daemon=True) |
The process name will default to the format “Process-%d“, where “%d” is an integer for the child process number created by the parent process, counted from 1 sequentially up for each new process. This means that the first new process you might create would have the default name “Process-1“.
We can also configure the process name and whether it is a daemon after the multiprocessing.Process instance has been created via attributes, as long as the process has not started running (e.g. the start() function has not yet been called).
The “name” attribute can be used to set the process name.
For example:
1 2 3 4 5 |
... # create a process with default name and daemon process = Process() # set the name process.name = 'MyProcess' |
The “daemon” attribute can be used to set whether the process is a background process or not.
For example:
1 2 3 4 5 |
... # create a process with default name and daemon process = Process() # configure to be a background process process.daemon = True |
Now that we know how to configure a new process in Python, let’s look at some worked examples.
Example of Configuring Process Name
Processes can be assigned custom names.
The name of a process can be set via the “name” argument in the multiprocessing.Process constructor.
For example:
1 2 3 |
... # create a process with a custom name process = Process(name='MyProcess') |
The example below demonstrates how to set the name of a process in the process class constructor.
1 2 3 4 5 6 7 8 9 |
# SuperFastPython.com # example of setting the process name in the constructor from multiprocessing import Process # entry point if __name__ == '__main__': # create a process with a custom name process = Process(name='MyProcess') # report process name print(process.name) |
Running the example creates the child process with the custom name then reports the name of the process.
1 |
MyProcess |
The name of the process can also be set via the “name” property.
For example:
1 2 3 |
... # set the name process.name = 'MyProcess' |
The example below demonstrates this by creating an instance of a process and then setting the name via the property.
1 2 3 4 5 6 7 8 9 10 11 |
# SuperFastPython.com # example of setting the process name via the property from multiprocessing import Process # entry point if __name__ == '__main__': # create a process process = Process() # set the name process.name = 'MyProcess' # report process name print(process.name) |
Running the example creates the process and sets the name then reports that the new name was assigned correctly.
1 |
MyProcess |
Free Python Multiprocessing Course
Download your FREE multiprocessing PDF cheat sheet and get BONUS access to my free 7-day crash course on the multiprocessing API.
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.
Example of Configuring a Daemon Process
Processes can be configured to be “daemon” or “daemonic”, that is, they can be configured as background processes.
A parent Python process can only exit once all non-daemon child processes have exited.
This means that daemon child processes can run in the background and do not prevent a Python program from exiting when the “main parts of the program” have finished.
A process can be configured to be a daemon by setting the “daemon” argument to True in the multiprocessing.Process constructor.
For example:
1 2 3 |
... # create a daemon process process = Process(daemon=True) |
The example below shows how to create a new daemon process.
1 2 3 4 5 6 7 8 9 |
# SuperFastPython.com # example of setting a process to be a daemon via the constructor from multiprocessing import Process # entry point if __name__ == '__main__': # create a daemon process process = Process(daemon=True) # report if the process is a daemon print(process.daemon) |
Running the example creates a new process and configures it to be a daemon process via the constructor.
1 |
True |
We can also configure a process to be a daemon process after it has been constructed via the “daemon” property.
For example:
1 2 3 |
... # configure the process to be a daemon process.daemon = True |
The example below creates a new multiprocessing.Process instance then configures it to be a daemon process via the property.
1 2 3 4 5 6 7 8 9 10 11 |
# SuperFastPython.com # example of setting a process to be a daemon via the property from multiprocessing import Process # entry point if __name__ == '__main__': # create a process process = Process() # configure the process to be a daemon process.daemon = True # report if the process is a daemon print(process.daemon) |
Running the example creates a new process instance then configures it to be a daemon process, then reports the daemon status.
1 |
True |
Overwhelmed by the python concurrency APIs?
Find relief, download my FREE Python Concurrency Mind Maps
Further Reading
This section provides additional resources that you may find helpful.
Python Multiprocessing Books
- Python Multiprocessing Jump-Start, Jason Brownlee (my book!)
- Multiprocessing API Interview Questions
- Multiprocessing API Cheat Sheet
I would also recommend specific chapters in the books:
- Effective Python, Brett Slatkin, 2019.
- See: Chapter 7: Concurrency and Parallelism
- High Performance Python, Ian Ozsvald and Micha Gorelick, 2020.
- See: Chapter 9: The multiprocessing Module
- Python in a Nutshell, Alex Martelli, et al., 2017.
- See: Chapter: 14: Threads and Processes
Guides
- Python Multiprocessing: The Complete Guide
- Python Multiprocessing Pool: The Complete Guide
- Python ProcessPoolExecutor: The Complete Guide
APIs
References
Takeaways
You now know how to configure a new multiprocessing.Process instance.
Do you have any questions?
Ask your questions in the comments below and I will do my best to answer.
Photo by iSAW Company on Unsplash
Do you have any questions?