假设我有一个服务:
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 版。我怎样才能检测到这种情况并避免客户端挂起?