Catch线程中断系统调用

时间:2012-11-16 09:47:29

标签: python multithreading asyncsocket

我有一个使用envisage框架的客户端 - 服务器应用程序,我使用线程来处理连接,这里是来自代码的令牌

    ....
    SocketServer.TCPServer.allow_reuse_address = True        
    self.server = TCPFactory( ( HOST, PORT ), TCPRequestHandler, self.application)               

    self.server_thread = threading.Thread( target = self.server.serve_forever )        

    self.server_thread.setDaemon( True )       
    self.server_thread.start() 

class TCPFactory( SocketServer.ThreadingTCPServer ):    
    def __init__( self, server_address, RequestHandlerClass, application ):
        SocketServer.ThreadingTCPServer.__init__( self, server_address, RequestHandlerClass )
        self.application = application

class TCPRequestHandler( SocketServer.BaseRequestHandler ):
    """"""
    def setup( self ):
        .....

在envisage框架中,我调用了open_file( )函数,它给了我们一个弹出窗口,但是当这个窗口出现时,我收到的是以下错误

Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 504, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/lib/python2.7/SocketServer.py", line 225, in serve_forever
    r, w, e = select.select([self], [], [], poll_interval)
error: (4, 'Interrupted system call')

如何处理此错误?

2 个答案:

答案 0 :(得分:3)

在Armin Rigo评论之后,我修改了SockeServer.py

def serve_forever(self, poll_interval=0.5):
        """Handle one request at a time until shutdown.

        Polls for shutdown every poll_interval seconds. Ignores
        self.timeout. If you need to do periodic tasks, do them in
        another thread.
        """
        self.__is_shut_down.clear()
        try:
            while not self.__shutdown_request:
                # XXX: Consider using another file descriptor or
                # connecting to the socket to wake this up instead of
                # polling. Polling reduces our responsiveness to a
                # shutdown request and wastes cpu at all other times.
                try:
                        r, w, e = select.select([self], [], [], poll_interval)
                except select.error  as ex:
                        #print ex
                        if ex[0] == 4:
                                continue
                        else:
                                raise  
                if self in r:
                    self._handle_request_noblock()
        finally:
            self.__shutdown_request = False
            self.__is_shut_down.set()

答案 1 :(得分:0)

当我向程序添加一个小httpd服务器时,我遇到了类似的问题,它接收来自其他进程的各种信号。在玩完之后我想出了一个简单的解决方案,避免实际修改stlib代码,但我认为它有点冒险。我只是将一个serve_forever调用包装在一个捕获并忽略套接字错误的循环中:

def non_int_serve_forever(self, poll_interval=0.5):
   while 1:
        try:
            self.serve_forever(poll_interval=poll_interval)
            break
        except select.error:
            pass

这消除了为不同版本的SocketServer.py需要不同解决方案的风险,但是即使它现在可以正常工作,但很多时候,serve_forever()应该可以多次重启,这并不明显。

有什么想法吗?