gpt4 book ai didi

python - 在 ZeroRPC 中实现自定义队列

转载 作者:行者123 更新时间:2023-11-28 22:58:36 26 4
gpt4 key购买 nike

目前,我正在使用 ZeroRPC,我让“工作人员”连接到“服务器”并执行服务器发送给他们的工作。

目前,只要有调用,就会通过 ZeroRPC 进行调用,据我所知,它使用的是 FIFO 队列。

我想使用自己的队列来限制/优先调用。

我希望 ZeroRPC 公开一个 gevent Event,当它的内部队列运行为空时触发。

最佳答案

您要做的是在您的服务器中创建您自己的工作队列。并按照您希望的优先级自行调度调用。

由于几行代码表达的内容比 3 卷中的任何吸血鬼故事都多,让我们用伪代码看看服务器的样子:

myqueue = MySuperBadAssQueue()

def myqueueprocessor():
for request in myqueue: # blocks until next request
gevent.spawn(request.processme) # do the job asynchronously

gevent.spawn(myqueueprocessor) # do that at startup

class Server:

def dosomething(args...blabla...): # what users are calling
request = Request(args...blabla...)
myqueue.put(request) # something to do buddy!
return request.future.get() # return when request is completed
# (can also raise an exception)

# An example of what a request could look like:
class Request:
def __init__(self, ....blablabla...):
self.future = gevent.AsyncResult()

def process():
try:
result = someworker(self.args*) # call some worker
self.future.set(result) # complete the initial request
except Exception as e:
self.future.set_exception(e)

由 MySuperBadAssQueue 来完成所有智能工作,如果需要,可以节流,必要时取消带有异常的请求,等等...

ZeroRPC 不会公开任何事件让您知道它的“内部”队列是否运行空:

In fact, there is no explicit queue in ZeroRPC. What happens, is simply first come first serve, and the exact order depend both of ZeroMQ and the Gevent IOLoop (libevent or libev depending of the version). It happens that in practice, this conveniently plays like a FIFO queue.

关于python - 在 ZeroRPC 中实现自定义队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13724387/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com