grpc数据传输限制|python端流传输慢问题

python grpc 数据传输大小限制

# 客户端数据传输大小配置
MAX_MESSAGE_LENGTH = 1024 * 1024 * 1024
options = [
    ('grpc.max_send_message_length', MAX_MESSAGE_LENGTH),
    ('grpc.max_receive_message_length', MAX_MESSAGE_LENGTH)
]
channel = grpc.aio.insecure_channel('192.168.70.83:50003', options=options)

# 服务端传输大小配置
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10), options = [
        ('grpc.max_send_message_length', MAX_MESSAGE_LENGTH),
        ('grpc.max_receive_message_length', MAX_MESSAGE_LENGTH)
    ])

python grpc stream 流数据传输慢问题

流式rpc在接收数据和消息发送创建了额外的线程,这使得流式rpc比一元rpc在python的grpc数据传输中更慢,这一点跟grpc支持的其他语言是不同的。官方推荐使用 asyncio 提升性能。

grpc_asyncio 地址:https://grpc.github.io/grpc/python/grpc_asyncio.html

grpc aio test 地址: https://github.com/grpc/grpc/blob/5d0e744da62c8680ef81fd5f51f59559cbd2b590/src/python/grpcio_tests/tests_aio/unit/call_test.py#L40

客户端demo:

def run():
    MAX_MESSAGE_LENGTH = 1024 * 1024 * 1024
    options = [
        ('grpc.max_send_message_length', MAX_MESSAGE_LENGTH),
        ('grpc.max_receive_message_length', MAX_MESSAGE_LENGTH)
    ]
    channel = grpc.aio.insecure_channel('192.168.70.83:50003', options=options)
    stub = xxx_pb2_grpc.xxxInterfaceStub(channel)
    request = xxx_pb2.xxx(name="123")
    call = stub.Hello(request)
    response = await call.read()

if __name__ == '__main__':
    import asyncio
    asyncio.run(run())
posted @ 2022-08-09 17:00  KbMan  阅读(1273)  评论(0编辑  收藏  举报