本文介绍了如何将zerorpc作为greenlet运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在同一循环中将zeroRPC服务器作为greenlet与其他gevent greenlet一起运行.ZeroRPC的文档有些许不足.这是启动zeroRPC服务器的建议方法:

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

要将服务器作为Greenlet运行,我将运行打包在Greenlet中:

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.
# ...

但是考虑到zeroRPC已经基于gevent,并且gevent框架中的其他服务器具有非阻塞启动方法,这似乎有点尴尬.

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.

有更好的方法吗?

推荐答案

这是最好的方法.

.run()方法将负责设置(zerorpc)服务器,并根据需要生成和管理任何子greenlet.这样可以有效地创建一棵绿色的树,将所有致命错误冒泡回到.run()方法中. zerorpc服务器将在新的greenlet中运行任何传入的请求,这些新的greenlet是从.run()方法拥有的greenlet树中产生的.

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.

具有阻塞式.run()方法,可让您通过简单的try/catch处理.run()引发的错误.此外,当.run()返回时,表示Zerorpc服务器已完全停止.例如,当您从另一个greenlet调用.stop()时,zerorpc服务器将停止接受新请求并完成处理活动请求,然后再从.run()返回

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

这篇关于如何将zerorpc作为greenlet运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:28