0

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!!!

toti08
  • 2,448
  • 5
  • 24
  • 36

1 Answers1

0

Looks like your internal loop is blocking progBreak check, this code is working for me:

import signal

progBreak = False

def newBreakHandler(signum, frame):
    global progBreak
    progBreak = True


def runMain():
    global progBreak

    signal.signal(signal.SIGINT, newBreakHandler)


    while not progBreak:
        while not progBreak and client.poll():
            command = client.recv_bytes()

    print "Exiting..."

runMain()
bakatrouble
  • 1,746
  • 13
  • 19
  • The thing is that this simple code works even if you put an internal loop inside it, so I guess there's something else in the program that is going wrong. I'll update the question to make it more specific, thanks for now! – toti08 Jul 11 '17 at 11:39
  • I have modified my question, the problem is what I am doing inside the inner while loop... – toti08 Jul 11 '17 at 11:55
  • While `procBreak` doesn't become True (inner loop doesn't stop), outer loop condition isn't being checked, so the interpreter should exit inner loop first in order to "see" that outer loop should also be stopped. – bakatrouble Jul 11 '17 at 11:58
  • Try to add `progBreak` checking in inner loop too (`while not procBreak and not progBreak`) – bakatrouble Jul 11 '17 at 11:59
  • The `command = client.recv_bytes()` line is blocking execution then, I'm afraid you can't do anything about it. – bakatrouble Jul 11 '17 at 12:06
  • Check out `multiprocessing.Connection.poll()` then, it allows to check if there is any data to receive so `recv_bytes()` don't block execution waiting for nothing (added example to answer). – bakatrouble Jul 11 '17 at 12:09
  • Yes, I guess you're right, I was also checking the documentation. Thanks! – toti08 Jul 11 '17 at 12:18
  • If you modify your answer with this last comment I can accept it. – toti08 Jul 11 '17 at 12:22