Reputation: 2573
I want to run a zeroRPC server as a greenlet with other gevent greenlets in the same loop. The documentation of ZeroRPC is a little light. This is the suggested way to start a zeroRPC server:
s = zerorpc.Server(Cooler())
s.bind("tcp://0.0.0.0:4242")
s.run()
To run the server as a greenlet, I've wrapped the run in a greenlet:
s = zerorpc.Server(Cooler())
s.bind("tcp://0.0.0.0:4242")
gevent.spawn(s.run)
# More code and greenlets started.
# ...
But it seems a little awkward, considering that zeroRPC already is based on gevent, and that other servers in the gevent framework have a non-blocking start method.
Is there a better way to do this?
Upvotes: 1
Views: 1340
Reputation: 633
This is the best way to do it.
The .run() method will take care of setting up the (zerorpc) server, spawning and managing any sub-greenlets as needed. This effectively creates a tree of greenlet, bubbling up any fatal errors back to the .run() method. The zerorpc server will run any incoming request in a new greenlet, spawned from the tree of greenlet owned by the .run() method.
Having a blocking .run() method let you handle errors raised by .run() with a simple try/catch. Additionally, when .run() returns, it means the zerorpc server is fully stopped. For example, when you call .stop() from another greenlet, the zerorpc server will stop accepting new requests and finish processing active requests before returning from .run()
Upvotes: 1