Skip to main content
 首页 » 编程设计

python之通过 rpyc.Connection 检测停止的服务器进程

2025年05月04日11lovecherry

假设我有一个服务:

import rpyc 
 
class MyService(rpyc.Service): 
    my_dict = {} 
 
    def exposed_put(self, key, val): 
        MyService.my_dict[key] = val 
 
    def exposed_get(self, key): 
        return MyService.my_dict[key] 
 
    def exposed_delete(self, key): 
        del MyService.my_dict[key] 

现在我启动在 ThreadedServer 中运行的服务:

from rpyc.utils.server import ThreadedServer 
server = ThreadedServer(MyService, port=8000) 
server.start() 

现在在同一台机器上的不同进程中,我打开一个到服务器的新连接:

import rpyc 
c = rpyc.connect('localhost', 8000) 

...但是在访问连接的根之前,服务器进程由于某种原因停止了,例如服务器进程的控制终端中的 Ctrl-Z。现在,当我尝试通过以下方式访问根目录时:

c.root 

... Python 挂起。客户端的 Ctrl-C 显示:

In [31]: c.root 
^C--------------------------------------------------------------------------- 
KeyboardInterrupt                         Traceback (most recent call last) 
<ipython-input-31-856a441cc51a> in <module>() 
----> 1 c.root 
 
/home/mack/anaconda/lib/python2.7/site-packages/rpyc/core/protocol.pyc in root(self) 
    465         """Fetches the root object (service) of the other party""" 
    466         if self._remote_root is None: 
--> 467             self._remote_root = self.sync_request(consts.HANDLE_GETROOT) 
    468         return self._remote_root 
    469 
 
/home/mack/anaconda/lib/python2.7/site-packages/rpyc/core/protocol.pyc in sync_request(self, handler, *args) 
    436         seq = self._send_request(handler, args) 
    437         while seq not in self._sync_replies: 
--> 438             self.serve(0.1) 
    439         isexc, obj = self._sync_replies.pop(seq) 
    440         if isexc: 
 
/home/mack/anaconda/lib/python2.7/site-packages/rpyc/core/protocol.pyc in serve(self, timeout) 
    385                   otherwise. 
    386         """ 
--> 387         data = self._recv(timeout, wait_for_lock = True) 
    388         if not data: 
    389             return False 
 
/home/mack/anaconda/lib/python2.7/site-packages/rpyc/core/protocol.pyc in _recv(self, timeout, wait_for_lock) 
    342             return None 
    343         try: 
--> 344             if self._channel.poll(timeout): 
    345                 data = self._channel.recv() 
    346             else: 
 
/home/mack/anaconda/lib/python2.7/site-packages/rpyc/core/channel.pyc in poll(self, timeout) 
     41     def poll(self, timeout): 
     42         """polls the underlying steam for data, waiting up to *timeout* seconds""" 
---> 43         return self.stream.poll(timeout) 
     44     def recv(self): 
     45         """Receives the next packet (or *frame*) from the underlying stream. 
 
/home/mack/anaconda/lib/python2.7/site-packages/rpyc/core/stream.pyc in poll(self, timeout) 
     39             while True: 
     40                 try: 
---> 41                     rl, _, _ = select([self], [], [], timeout) 
     42                 except select_error as ex: 
     43                     if ex[0] == errno.EINTR: 
 
KeyboardInterrupt: 

所以看起来是对 Stream.poll 的调用如果服务器进程已停止但仍处于连接状态(底层套接字仍处于打开状态),则会陷入无限循环。我认为这是 Stream 实现中的意外情况是否正确?我使用的是 3.3.0 版。我如何检测这种情况并避免客户端挂起?

请您参考如下方法:

如果服务器有被攻击的风险,你可以检查 c.closed 的值,你也可以在你的客户端提供一个回调函数来通知你它已经关闭并且将它传递给您的初始化程序,名称可能为 on_exit,然后用 atexit 注册它.

要处理远程服务器暂停或不太远程、繁忙的可能性,您必须实现心跳,即定期回调以通知客户端服务器可用。