문제

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!

도움이 되었습니까?

해결책

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.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top