How to interrupt a blocking system call function from a signal handler in Python?

Viewed 600

I have a thread being generated from the main one which has basically an infinite loop with a system blocking function: something like:

def run(self):
    global EXIT
    while not EXIT:
        data = self.conn.recv(1024)
        ...

I have defined a signal handler for SIGINT

def sig_handler(signum, frame):
    global EXIT, threads
    if (signum == 2):#defensive
        print("Called SIGINT")
        EXIT = True

Being the signal catched by the main thread, it interrupts the main thread. However the other thread is stuck on the blocking function: is there a way to interrupt a system call blocking function in python so that exiting from this function?

I do not want to stop the process directly how SIGINT normally does but I would like just to interrupt recv so that the condition of while is not true anymore and I can do other stuff before exiting.

0 Answers
Related