Last Updated on September 12, 2022
If a Python program has one or more non-daemon processes running, it cannot exit, even if forceful attempts to exit are made such as by calling sys.exit().
In this tutorial you will discover whether a running child process can prevent a program from exiting in Python.
Let’s get started.
Does the Program Exit If Child Process Still Running
The multiprocessing module provides process-based concurrency, allowing us to execute code in a child process.
This can be achieved by creating a new instance of the multiprocessing.Process class and specificity the function to execute in a child process via the “target” argument.
Once configured, the child process can be started by calling the start method.
For example:
1 2 3 4 5 |
... # configure a child process process = Process(target=task) # start the process process.start() |
The main process may then proceed with other tasks.
This raises the question:
What happens when the main process finishes and attempts to exit while the child process is running?
Will the running child process prevent the program from exiting?
We can explore this question with a worked example.
Run loops using all CPUs, download your FREE book to learn how.
Example of Child Process Running After Main Process Finished
We can explore what happens if we leave a child process running after the main process is finished.
In this example, we will define a custom function to execute in a child process that will run in a loop forever. The main process will then execute the function in a child process then attempt to exit.
The complete example is listed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# SuperFastPython.com # example of a child process preventing the main process from terminating from time import sleep from multiprocessing import Process # custom task function executed in a child process def task(): # run forever while True: # block sleep(1) # protect the entry point if __name__ == '__main__': # configure and start a child process Process(target=task).start() # report a message print('Child process is running, main is done') |
Running the example configures and starts a child process to execute our custom function that loops forever.
The child process starts running and runs forever.
The main process then reports a message then attempts to terminate.
We can see that the main process is prevented from exiting by the running child process.
Note, the program will have to be forcefully terminated, such as by pressing CONTROL-C.
1 |
Child process is running, main is done |
Example of the Main Process Calling sys.exit()
The previous example demonstrated that the program is not terminated if at least one child process is still running.
This raises the question, what happens if the main process attempts to forcefully terminate the program by calling sys.exit()?
Will this terminate the program, or only terminate the main process, leaving the child process running?
We can update the previous example so that the main process calls sys.exit() after starting the child process.
1 2 3 |
... # attempt to exit forcefully exit(1) |
The complete example is listed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# SuperFastPython.com # example of a child process preventing the main process from terminating from sys import exit from time import sleep from multiprocessing import Process # custom task function executed in a child process def task(): # run forever while True: # block sleep(1) # protect the entry point if __name__ == '__main__': # configure and start a child process Process(target=task).start() # report a message print('Child process is running, main is done') # attempt to exit forcefully exit(1) |
Running the example configures and starts a child process to execute our custom function that loops forever.
The child process starts running and runs forever.
The main process then reports a message then attempts to terminate forcefully by calling sys.exit().
We can see that the main process is still prevented from exiting by the running child process, even when attempting to terminate the program forcefully.
Note, the program will have to be forcefully terminated, such as by pressing CONTROL-C.
1 |
Child process is running, main is done |
You can learn more about terminating processes with sys.exit() in the tutorial:
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.
Exit the Program By Using a Daemon Child Process
If we require that child processes terminate when the main process is finished, the solution is to use daemon child processes.
A daemon process is a background process, intended specifically to exit when the main process terminates.
We can configure a child process to be a daemon process by setting the “daemon” argument to True when configuring a new child process.
For example:
1 2 3 |
... # configure and start a child process Process(target=task, daemon=True).start() |
Tying this together, the complete example is listed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# SuperFastPython.com # example of creating a daemon process that does not prevent the main process from exiting from time import sleep from multiprocessing import Process # custom task function executed in a child process def task(): # run forever while True: # block sleep(1) # protect the entry point if __name__ == '__main__': # configure and start a child process Process(target=task, daemon=True).start() # report a message print('Child process is running, main is done') |
Running the example configures and starts a child process to execute our custom function that loops forever.
The child process starts running and runs forever.
The main process then reports a message then attempts to exit.
In this case, we can see that the program does terminate as expected. The daemon child process does not prevent the main process from exiting.
1 |
Child process is running, main is done |
You can learn more about daemon processes in the tutorial:
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 that child processes can prevent a program from exiting in Python.
Do you have any questions?
Ask your questions in the comments below and I will do my best to answer.
Photo by frank mckenna on Unsplash
Do you have any questions?