python SIGTERM not work when I use multiprocessing Pool -
correct code expected: multiprocessing import pool import signal import time import os
def consumer(i): while true: # print os.getpid() pass def handler(signum, frame): print 'here go' signal.signal(signal.sigterm, handler) p = pool(5) p.map_async(consumer, [1 in range(5)]) while true: pass p.terminate() # p.close() p.join() ==================================================
i have found problem, when use map function, main func blocked, , signal handler called when map function funished. so, use "map_async" function more better fix problem.
here found:
a long-running calculation implemented purely in c (such regular expression matching on large body of text) may run uninterrupted arbitrary amount of time, regardless of signals received. python signal handlers called when calculation finishes.
==================================================
i wrote program following, , expect exit/(like program print string) in program when type "kill pid" in terminal, not work. there other strategy block sigterm in main func.
from multiprocessing import pool import signal import time import os def consumer(i): while true: # print os.getpid() pass def handler(signum, frame): print 'here go' signal.signal(signal.sigterm, handler) p = pool(5) p.map(consumer, [1 in range(5)]) p.terminate() # p.close() p.join()
you need use map_async method map 1 blocks until results not ready. therefore, in example call terminate never reached.
Comments
Post a Comment