I wrote a test program, which has two processes. The father process gets data from a Queue, and the child puts data into it. There is a signal handler which tells the program to exit. However, it does not exit sometimes when I send the signal SIGTERM to the pid(child process) I printed, and it seems to be having a deadlock.
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()