2

我在我的程序中使用 python 2.7 和 zerorpc 和线程。当我通过 zerorpc 在 Thread 对象中调用远程函数时,出现异常“LoopExit:此操作将永远阻塞”。

我的代码示例:

class Reminder(Thread):
def __init__(self):
    super(Reminder, self).__init__()
    self.rpc_client = zerorpc.Client('tcp://10.10.10.3:10001')
def run(self):
    while True:
        try:
            self.rpc_client.alarm_by_wx('alive', 'zhanghe') # the exception arise here
        except Exception, e:
            logger.exception('rpc error')
        time.sleep(24*60*60)
if __name__ == '__main__':
    r = Reminder()
    r.start()
    r.join()

例外:

Traceback (most recent call last):
  File "api_monitor.py", line 92, in run
    self.rpc_client.alarm_by_wx('alive', 'zhanghe')
  File "/usr/local/lib/python2.7/dist-packages/zerorpc/core.py", line 257, in <lambda>
    return lambda *args, **kargs: self(method, *args, **kargs)
  File "/usr/local/lib/python2.7/dist-packages/zerorpc/core.py", line 246, in __call__
    bufchan.emit_event(request_event)
  File "/usr/local/lib/python2.7/dist-packages/zerorpc/channel.py", line 235, in emit_event
    self._channel.emit_event(event)
  File "/usr/local/lib/python2.7/dist-packages/zerorpc/heartbeat.py", line 116, in emit_event
    self._channel.emit_event(event, timeout)
  File "/usr/local/lib/python2.7/dist-packages/zerorpc/channel.py", line 155, in emit_event
    self._multiplexer.emit_event(event, timeout)
  File "/usr/local/lib/python2.7/dist-packages/zerorpc/channel.py", line 67, in emit_event
    return self._events.emit_event(event, timeout)
  File "/usr/local/lib/python2.7/dist-packages/zerorpc/events.py", line 328, in emit_event
    self._send(parts, timeout)
  File "/usr/local/lib/python2.7/dist-packages/zerorpc/events.py", line 125, in __call__
    self._send_queue.put(parts, timeout=timeout)
  File "/usr/local/lib/python2.7/dist-packages/gevent/queue.py", line 447, in put
    result = waiter.get()
  File "/usr/local/lib/python2.7/dist-packages/gevent/hub.py", line 575, in get
    return self.hub.switch()
  File "/usr/local/lib/python2.7/dist-packages/gevent/hub.py", line 338, in switch
    return greenlet.switch(self)
LoopExit: This operation would block forever

如果我像下面的代码一样在主线程中调用 rpc 函数,它会毫无例外地运行。

if __name__ == '__main__':
    rpc_client = zerorpc.Client('tcp://10.10.10.3:10001')
    while True:
        try:
            rpc_client.alarm_by_wx('alive', 'zhanghe') # the exception arise here
        except Exception, e:
            logger.exception('rpc error')
        time.sleep(24*60*60)

出了什么问题?zerorpc 可以在多线程环境中工作吗?感谢您的任何想法。

4

1 回答 1

0

由于 zerorpc 使用 gevent 库,您不能从用于与服务器建立原始连接的不同线程调用客户端方法。

您可以通过如下修改使您的代码工作:

class Reminder(Thread):
def __init__(self):
    super(Reminder, self).__init__()
def run(self):
    self.rpc_client = zerorpc.Client('tcp://10.10.10.3:10001')
    while True:
        try:
            self.rpc_client.alarm_by_wx('alive', 'zhanghe') # no exception should arise here
        except Exception, e:
            logger.exception('rpc error')
        time.sleep(24*60*60)
if __name__ == '__main__':
    r = Reminder()
    r.start()
    r.join()
于 2019-12-07T21:56:50.357 回答