I have a python script which I'd like to stop by sending a "SIGINT" signal using the command line:
kill -2 <PID>
The script spawns a child process and then enters an infinite loop where it tries to receive data from a socket. I have installed the signal handler, but it looks like my program is not catching the signal. Here is my code:
import signal
import multiprocessing
def newBreakHandler(signum, frame):
global progBreak
progBreak = True
def runMain():
client = multiprocessing.Client(<address>)
# ......
# Insert break handler
signal.signal(signal.SIGINT, newBreakHandler)
while not progBreak:
try:
# Some other stuff...
# ...
childProc = multiprocessing.Process(target=childProcName)
childProc.start()
# Main loop
while not progBreak:
command = client.recv_bytes()
except:
pass
print "Exiting..."
The problem is that whenever I send a:
kill -2 <PID>
I never see the "Exiting" text printed and the PID is not killed. I think this is because the inner "while" loop is busy waiting for new data from the client, but if the client is not sending new data.
Is there a way to solve the problem?
TIA!!!