简体   繁体   中英

How to run zerorpc as a greenlet?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM