简体   繁体   English

如何将zerorpc作为greenlet运行?

[英]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. 我想在同一循环中将zeroRPC服务器作为greenlet与其他gevent greenlet一起运行。 The documentation of ZeroRPC is a little light. ZeroRPC的文档介绍得很少。 This is the suggested way to start a zeroRPC server: 这是启动zeroRPC服务器的建议方法:

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: 要将服务器作为greenlet运行,我将运行打包在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. 但是考虑到zeroRPC已经基于gevent,并且gevent框架中的其他服务器都具有非阻塞启动方法,这似乎有点尴尬。

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. .run()方法将负责设置(zerorpc)服务器,并根据需要生成和管理任何子greenlet。 This effectively creates a tree of greenlet, bubbling up any fatal errors back to the .run() method. 这有效地创建了greenlet树,将所有致命错误冒泡回到.run()方法。 The zerorpc server will run any incoming request in a new greenlet, spawned from the tree of greenlet owned by the .run() method. zerorpc服务器将在新的greenlet中运行任何传入的请求,这些新的greenlet是从.run()方法拥有的greenlet树中产生的。

Having a blocking .run() method let you handle errors raised by .run() with a simple try/catch. 具有阻塞的.run()方法使您可以通过简单的try / catch处理.run()引发的错误。 Additionally, when .run() returns, it means the zerorpc server is fully stopped. 此外,当.run()返回时,表示Zerorpc服务器已完全停止。 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() 例如,当您从另一个greenlet调用.stop()时,zerorpc服务器将停止接受新请求并完成处理活动请求,然后再从.run()返回

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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