2011-09-08 27 views
1

我对Python非常陌生,所以如果这个问题非常基本,请原谅我。在缓慢的系统调用中处理SIGINT

我想在使用select模块接受来自套接字的数据时处理键盘中断。所以,我有一个select.select()函数调用来等待来自套接字的数据,然后将其输出到控制台。

当按下CTRL_C时,似乎有时会得到一个select.error,有时还会出现exceptions.IOError异常。对于这两个例外,相应的错误代码都是4。

有一些代码在调用堆栈中深入处理KeyboardInterrupt异常,因此当我在接受套接字连接的函数中获取SIGINT时,我只是想重新引发KeyboardInterrupt异常。我也想捕捉连接相关的异常。

检查异常的错误代码是否安全?如果它是4则引发KeyboardInterrupt?这会影响我捕捉连接相关异常的能力吗?有错误代码的好资源吗?

谢谢!

回答

1

改为使用errno.EINTR。这是安全的。

>>> import errno 
>>> errno.EINTR 
4 

然而,这并不会告诉你哪些信号中断的系统调用,只有一些信号中断了。

人2选择

 
EBADF An invalid file descriptor was given in one of the sets. (Per‐ 
     haps a file descriptor that was already closed, or one on which 
     an error has occurred.) 

EINTR A signal was caught; see signal(7). 

EINVAL nfds is negative or the value contained within timeout is 
     invalid. 

ENOMEM unable to allocate memory for internal tables. 
+0

谢谢您的回答。现在尝试。 – ravil