I'm using multiprocessing.Pool to spawn 2 child processes:
from multiprocessing import Pool
import os
import time
def job(job_id: int):
pid = os.getpid()
print(f"{pid} started {job_id}")
while True:
print(f"{pid} sleeping")
time.sleep(5)
def main() -> None:
pool = Pool(2)
for job_id in range(2):
pool.apply_async(job, [job_id])
pid = os.getpid()
while True:
print(f"Main process with pid {pid} sleeping")
time.sleep(5)
if __name__ == "__main__":
main()
Running the above code outputs:
Main process with pid 27769 sleeping
27770 started 0
27770 sleeping
27771 started 1
27771 sleeping
Main process with pid 27769 sleeping
27770 sleeping
27771 sleeping
If I send SIGINT to the main process kill -s SIGINT 27769, the entire operation is stopped (which is what I want) and the following error message shows up:
Traceback (most recent call last):
File "/home/leonarduschen/src/main.py", line 26, in <module>
main()
File "/home/leonarduschen/src/main.py", line 22, in main
time.sleep(5)
KeyboardInterrupt
However, if I send SIGTERM to the main process kill -s SIGTERM 27769, only the main process is killed. The message terminated is printed and my terminal is freed, but the child processes are still running and printing:
27770 sleeping
27771 sleeping
Main process with pid 27769 sleeping
Terminated
(venv) :~/src/$ 27770 sleeping
27771 sleeping
I cannot find any information about this in multiprocessing documentation. Why are the 2 signals handled differently?