由于gevent / grpc兼容性问题已得到修复,我试图使用它。
我用一个示例脚本测试了它
from gevent import monkey
monkey.patch_all()
import grpc._cython.cygrpc
grpc._cython.cygrpc.init_grpc_gevent()
import grpc
import time
import sys
channel = grpc.insecure_channel('localhost:5000')
stub =hello_word_pb2_grpc.HelloWordStub(channel)
data = hellow_word_pb2.HelloWorld()
num_requests=3000
start=time.time()
futures = []
# Make async requests
for i in range (num_requests):
futures.append(stub.HelloWorld.future(req))
# Wait for the requests to complete
for i in range (num_requests):
try:
result = futures[i].result()
# Do something with the result
except Exception as e:
print(e)
end = time.time()
print(end - start)
现在没有这个补丁
import grpc._cython.cygrpc
grpc._cython.cygrpc.init_grpc_gevent()
完成3000个req需要0.456701040268秒
现在有补丁
import grpc._cython.cygrpc
grpc._cython.cygrpc.init_grpc_gevent()
需要1.06秒。
任何有关兼容性补丁可能出错的建议。
显然,如果我将reqs的数量减少到1000并且在每次调用中使用1000个异步req进行3次调用,则总计3000个req所需的时间小于1.06。但是我想知道导致修补的原因是什么?
我在这里找到了类似的东西 - https://github.com/grpc/grpc/blob/master/src/python/grpcio_tests/commands.py#L115
它有关系吗?