我对 Python 很陌生,所以如果这个问题非常基本,请原谅我。
我试图在使用选择模块从套接字接受数据时处理键盘中断。因此,我有一个 select.select() 函数调用来等待来自套接字的数据,然后将其输出到控制台。
当按 CTRL_C 时,似乎有时会出现 select.error,有时会出现 exceptions.IOError 异常。对于这两种异常,相应的错误代码都是 4。
在调用堆栈的更深处有一些处理 KeyboardInterrupt 异常的代码,因此当我在接受套接字连接的函数中收到 SIGINT 时,我只想重新引发 KeyboardInterrupt 异常。我还想捕获与连接相关的异常。
检查异常的错误代码并在错误代码为 4 时引发 KeyboardInterrupt 是否安全?这会影响我捕获与连接相关的异常的能力吗?有关于错误代码的好资源吗?
谢谢!
I am very new to Python, so forgive me if this question is very basic.
I am trying to handle a keyboard interrupt while accepting data from a socket using select module. So, I have a select.select() function call to wait for data from the socket, and then output it to console.
When pressing CTRL_C, it seems that sometimes I get a select.error, and sometimes exceptions.IOError exception. The corresponding error code is 4 for both exceptions.
There is some code that handles KeyboardInterrupt exception deeper in the call stack, so when I get a SIGINT in the function where I accept a socket connection, I just want to re-raise KeyboardInterrupt exception. I also want to catch connection related exceptions.
Would it be safe to check for exception's error code, and raise KeyboardInterrupt if it is 4? Will this affect my ability to catch connection-related exceptions? Is there a good resource on error codes?
Thanks!
发布评论
评论(1)
使用 errno.EINTR 代替。这是安全的。
但是,这不会告诉您哪个信号中断了系统调用,只是告诉您某个信号中断了它。
从man 2选择:
Use
errno.EINTR
instead. It is safe.However, this will not tell you which signal interrupted the system call, only that some signal interrupted it.
From man 2 select: