I want a script to start a new process, such that the new process continues running after the initial script exits. I expected that I could use multiprocessing.Process
to start a new process, and set daemon=True
so that the main script may exit while the created process continues running.
But it seems that the second process is silently terminated when the main script exits. Is this expected behavior, or am I doing something wrong?
How to Start a Process in Python? To start a new process, or in other words, a new subprocess in Python, you need to use the Popen function call. It is possible to pass two parameters in the function call. The first parameter is the program you want to start, and the second is the file argument.
Processes can only be started within the process they were created in. In the code provided, process2 was created in the main process, and yet tried to be started within another one ( process1 ). Also, processes cannot be restarted, so they should be created each time . start is used.
multiprocessing. freeze_support() This function will allow a frozen program to create and start new processes via the multiprocessing. Process class when the program is frozen for distribution on Windows. If the function is called and the program is not frozen for distribution, then it has no effect.
From the Python docs:
When a process exits, it attempts to terminate all of its daemonic child processes.
This is the expected behavior.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With