在缓慢的系统调用中处理 SIGINT

发布于 2024-12-03 19:22:36 字数 434 浏览 4 评论 0原文

我对 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城泪 2024-12-10 19:22:36

使用 errno.EINTR 代替。这是安全的。

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

但是,这不会告诉您哪个信号中断了系统调用,只是告诉您某个信号中断了它。

ma​​n 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.

Use errno.EINTR instead. It is safe.

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

However, this will not tell you which signal interrupted the system call, only that some signal interrupted it.

From man 2 select:

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文