Why does python program sometimes fail to exit?

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() 
+6
source share
1 answer

If you are not using python 3, you should use xrange instead of range for a large loop. Python tends to throttle when it exceeds a certain size of the list, and so you really need to go to the generators at that point.

This may very well be the problem you are seeing right now.

+1
source

Source: https://habr.com/ru/post/890915/


All Articles