I have a multiprocessing.Pool
running tasks that I wan't to exit gracefully in case of a terminate by handling the SIGTERM
signal
This is my code example (used python 3.9)
import os
import signal
import time
from multiprocessing import Pool
class SigTermException(Exception):
pass
def sigtermhandler(signum, frame):
raise SigTermException('sigterm')
def f():
print(os.getpid())
try:
while True:
print("loop")
time.sleep(5)
except SigTermException:
print("Received SIGTERM")
def main():
signal.signal(signal.SIGTERM, sigtermhandler)
pool = Pool()
pool.apply_async(f)
print("wait 5")
time.sleep(5)
print("Terminating")
pool.terminate()
print("Joining")
pool.join()
print("Exiting")
if __name__ == '__main__':
main()
I was expecting to print
...
Terminating
Received SIGTERM
Joining
Exiting
However it seems it doesn't go past pool.terminate()
Here's an example
wait 5
92363
loop
Terminating
loop
Received SIGTERM
Performing a ps
I see the following
92362 pts/0 S+ 0:00 | | \_ python signal_pool.py
92363 pts/0 S+ 0:00 | | \_ python signal_pool.py
So it looks like the child process is still 'alive'
Also tested the solution mentioned here to no avail
Any hints o help is appreciated