Reading the documentation on the daemon attribute for the multiprocessing module, I got the idea that when any process exits, their daemon threads would be terminated…
However, in the small programme below, the ‘grand’-child process does not get terminated, and is allowed to print “Finishing Process…”.
Why is that?
import multiprocessing as mp, time
def pid_task(sec: int):
print(f"Running Process:{mp.current_process().name}")
time.sleep(sec)
print(f"Finishing Process:{mp.current_process().name}")
def create_child(sec):
child = mp.Process(target=pid_task, args=[sec], daemon=True)
child.start()
pid_task(sec)
p1 = mp.Process(target=create_child, args=[5])
p1.start()
pid_task(1)
print("Terminating Process-1 and MainProcess")
p1.terminate()
print("Terminated p1 and terminating MainProcess")
I’m using Python 3.12.