我有一个用select阻止读取的程序,但是我想让它在发送SIGINT信号时正常返回。并使用处理程序处理信号。遗憾的是,块程序不会从select系统调用返回,而是接收SIGINT和interupt。 任何人都可以帮我弄清问题是什么?非常感谢。
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.'
答案 0 :(得分:0)
您误解了程序的行为:它确实从select系统调用返回,但是您必须处理错误“Interrupted system call”。
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.'