Does a Child Process Stop a Python Program From Exiting
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:
...
# 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.
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.
# 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.
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.
...
# attempt to exit forcefully
exit(1)
The complete example is listed below.
# 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.
Child process is running, main is done
You can learn more about terminating processes with sys.exit() in the tutorial:
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:
...
# configure and start a child process
Process(target=task, daemon=True).start()
Tying this together, the complete example is listed below.
# 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.
Child process is running, main is done
You can learn more about daemon processes in the tutorial:
Takeaways
You now know that child processes can prevent a program from exiting in Python.
If you enjoyed this tutorial, you will love my book: Python Multiprocessing Jump-Start. It covers everything you need to master the topic with hands-on examples and clear explanations.