如何使用 python 增加 grpc 中的消息大小

How to increase message size in grpc using python

我正在使用 grpc 进行消息传递,并且正在测试一个简单的服务器和客户端。当我的邮件大小超过限制时,我会收到此错误。

grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with 
(StatusCode.INVALID_ARGUMENT,
 Received message larger than max (7309898 vs. 4194304))>

如何增加服务器端和客户端的邮件大小?

您不应增加邮件大小。
它会带来性能损失。
在实际情况下,实现分页以拆分太大的消息。

更改发送和接收的 message_length 即可。

channel = grpc.insecure_channel(
    'localhost:50051',
    options=[
        ('grpc.max_send_message_length', MAX_MESSAGE_LENGTH),
        ('grpc.max_receive_message_length', MAX_MESSAGE_LENGTH),
    ],
)

我有这个问题,我通过在客户端和服务器上设置 'grpc.max_send_message_length' 和 'grpc.max_receive_message_length' 解决了这个问题:

在客户端中(此代码片段归功于@Dumbo):

channel = grpc.insecure_channel(
    'localhost:50051',
    options=[
        ('grpc.max_send_message_length', MAX_MESSAGE_LENGTH),
        ('grpc.max_receive_message_length', MAX_MESSAGE_LENGTH),
    ],
)

在服务器中:

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)
    ])

添加到现有答案中,您可以在 github 存储库中找到所有 key-value 对选项的列表 - 见下文

来自grpc-Glossary

channel_arguments A list of key-value pairs to configure the underlying gRPC Core channel or server object. Channel arguments are meant for advanced usages and contain experimental API (some may not labeled as experimental). Full list of available channel arguments and documentation can be found under the “grpc_arg_keys” section of “grpc_types.h” header file (https://github.com/grpc/grpc/blob/v1.43.x/include/grpc/impl/codegen/grpc_types.h). For example, if you want to disable TCP port reuse, you may construct channel arguments like: options = (('grpc.so_reuseport', 0),).