服务器
假设我有以下 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