2011-09-08 24 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