[英]Architecture recommendation for cancellable backend calls [zerorpc if possible?]
[英]Zerorpc asynchronous calls
假设我有以下Zerorpc服务器
#server side
import zerorpc
class API():
def long_running_task(self):
print('1 - started long_running_task')
#for instance a long running SQL query
zerorpc.gevent.sleep(10)
print('2 - finished long_running_task')
def other_task(self):
print('1 - started other_task')
pass
print('2 - finished other_task')
s = zerorpc.Server(API())
s.bind("tcp://0.0.0.0:4444")
zerorpc.gevent.spawn(s.run)
while True:
zerorpc.gevent.sleep(10)
和一个客户端应用程序,可同时发送多个请求
import zerorpc
client = zerorpc.Client()
client.connect("tcp://127.0.0.1:4444")
client.long_running_task(async_=True)
client.other_task(async_=True)
client.close()
问题在于,在执行long_running_task时,other_task不会开始运行。
所需的输出是:
1 - started long_running_task
1 - started other_task
2 - finished other_task
2 - finished long_running_task
代替
1 - started long_running_task
2 - finished long_running_task
1 - started other_task
2 - finished other_task
这里有一些错误
async_
应该是async
。 这样可以确保客户端方法立即返回 gevent.spawn(s.run)
启动服务器。 还可以保持您的主要greenlet运行。 zerorpc.gevent.spawn(s.run)
while True:
zerorpc.gevent.sleep(10)
API()
方法中,您也需要合作。 取而代之的time.sleep()
使用gevent.sleep()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.