Reputation: 2227
I have problem with launching zeroRPC server in python. I did it according to official example, but when I call run() method it works in endless loop, so my program can't continue after launching this server. I tried to run it in new thread but I got following exception:
LoopExit: ('This operation would block forever', <Hub at 0x7f7a0c8f37d0 epoll pending=0 ref=0 fileno=19>)
I really don't know how to fix it. Have any ideas ?
Upvotes: 1
Views: 1353
Reputation: 633
In short, you cannot use os threads with zerorpc.
Longer answer: zerorpc-python uses gevent for IO. This means your project MUST use gevent and be compatible with it. Native OS threads and gevent coroutines (also called greenlet, green threads etc) are not really friends.
There is a native threadpool option available in gevent (http://www.gevent.org/gevent.threadpool.html).
You cannot spawn a native OS thread and run gevent coroutines in there (including zerorpc).
If all you are doing works with gevent coroutines, then instead of running the run()
in a native thread, run it in a gevent coroutine/greenlet/greenthread like so:
# starts the server in its own greenlet
gevent.spawn(myserver.run)
# zerorpc will spawn many more greenlet as needed.
# they all need to run cooperatively
# here we are continuing on the main greenlet.
# as a single greenlet can execute at a time, we must never block
# for too long. Using gevent IOs will cooperatively yield for example.
# Calling gevent.sleep() will yield as well.
while True:
gevent.sleep(1)
Note: in case when gevent is not an option, a solution would be to implement a version of zerorpc-python that does not use gevent and implements its IO outside of Python, but this has interesting complication, and its not happening soon.
Upvotes: 4