Program using zerorpc and threading throws Exception "LoopExit: This operation would block forever"

635 views Asked by At

I'm using python 2.7 and zerorpc and threading in my program. When I called a remote function by zerorpc in a Thread object, Exception "LoopExit: This operation would block forever" arised.

an example of my code:

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()

exception:

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

if I call the rpc function in main thread like the code bellow, it runs without exception.

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)

What goes wrong? Can zerorpc work in multi-threading evnironment? Thanks for any ideas.

1

There are 1 answers

0
Joe Savage On

Due to zerorpc's usage of the gevent library, you cannot call a client method from a different thread that was used to make the original connection to the server.

You can make your code work by modifying it as follows:

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()