I wrote a test program that has two processes. The father process receives data from the queue, and the child places the data in it. There is a signal handler that tells the program about the exit. However, sometimes it does not exit when I send a SIGTERM signal to the pid (child process) that I printed, and it seems to be stuck.
import os import sys import multiprocessing import time import signal bStop = False def worker(que): signal.signal(signal.SIGTERM,sighandler) print 'worker:',os.getpid() for i in range(100000000): que.put(i) print 'STOP' def sighandler(num,frame): print 'catch signal' q.put('STOP') sys.exit(0) q = multiprocessing.Queue(100) p = multiprocessing.Process(target=worker,args=(q,)) p.start() for item in iter(q.get,'STOP'): print 'get',item pass print 'main stop' p.join()
source share