1

I have a program that block reading with select,but i want it to return gracefully with sending a SIGINT signal. and process that signal with a handler. unfortunately, the block program does not return from select system call, instead it receives SIGINT and interupt. can anybody help me with figuring out what is the problem? thanks a lot.

import select
import socket
import signal

class Test(object):
    def __init__(self):
        self._read, self._write = socket.socketpair()

    def wait(self):
        readable = [self._read.fileno()]
        return select.select(readable, [], readable)

    def wakeup(self):
        self._write.send('1')


t = Test()

def handler(signum, frame):
    t.wakeup()


signal.signal(signal.SIGINT, handler)

if __name__ == '__main__':
    print t.wait()
    print 'over.'
  • refer this http://stackoverflow.com/questions/2148888/python-trap-all-signals – Antarus May 29 '13 at 06:00
  • thanks for your help,but that refer does not help me out of the problem.i just curious why select does not get notified by os for readalbe fds,but gets interuppt signal instand:( – user2431104 May 29 '13 at 06:20

1 Answers1

0

You misinterpreted the program's behaviour: it did return from the select system call, but with the error 'Interrupted system call' that you have to handle.

import errno
import select
import socket
import signal

class Test(object):
    def __init__(self):
        self._read, self._write = socket.socketpair()

    def wait(self):
        readable = [self._read.fileno()]
        try: 
         return select.select(readable, [], readable)
        except select.error as e:
         if e.args[0] == errno.EINTR:
                return
         else:
                pass

    def wakeup(self):
        self._write.send('1')


t = Test()

def handler(signum, frame):
    t.wakeup()


signal.signal(signal.SIGINT, handler)

if __name__ == '__main__':
    print t.wait()
    print 'over.'
Armali
  • 18,255
  • 14
  • 57
  • 171