Scaling issues using gevent and grpc

被刻印的时光 ゝ 提交于 2021-01-25 10:43:47

问题


Since the gevent/grpc compatibility issue has been fixed, I was trying to use it.

I tested it out with a sample script

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)

Now without this patch

import grpc._cython.cygrpc
grpc._cython.cygrpc.init_grpc_gevent()

it takes 0.456701040268 seconds to finish 3000 reqs

Now with the patch

import grpc._cython.cygrpc
grpc._cython.cygrpc.init_grpc_gevent()

it takes 1.06 seconds.

Any suggestions what could go wrong with the compatibility patch.

Obviously if I decrease the number of reqs to 1000 and make 3 calls with 1000 async reqs in each call, the time it takes for the total 3000 reqs is lesser than 1.06. But I wanted to know what is causing the patching to make it so slow?

I found something similar mentioned here - https://github.com/grpc/grpc/blob/master/src/python/grpcio_tests/commands.py#L115

Is it related?


回答1:


In the standard gRPC Python implementation, asynchronous requests are performed on background threads. Pure Python programs can't get concurrency for CPU bound tasks because of the GIL, but because gRPC is a c-based library, a lot of the gRPC work is able to be done concurrently.

Enabling gEvent forces the entire program to run on a single thread. You loose the concurrency mentioned above. Additionally, there is some overhead in handling the IO in Python as opposed to c.

If your objective is to maximize performance, I'd recommend using the default gRPC implementation. gRPC gEvent is primarily intended for compatibility.



来源:https://stackoverflow.com/questions/51006950/scaling-issues-using-gevent-and-grpc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!