How to run zerorpc as a greenlet?

感情迁移 提交于 2019-12-11 10:46:45

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/19208535/how-to-run-zerorpc-as-a-greenlet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!