【问题标题】:How do I specify server options?如何指定服务器选项?
【发布时间】:2018-06-18 15:21:29
【问题描述】:

我正在尝试在 Python 中运行 gRPC 服务器。我找到了这样的方法:

import grpc
from concurrent import futures

server = grpc.server(futures.ThreadPoolExecutor(max_workers=100))
... # add my grpc servicer to server
server.add_insecure_port('[::]:50051')
server.start()

我需要向服务器添加一些选项,例如max_send_message_lengthmax_receive_message_length 等。grpc.server(...) 中有一个options 参数,但我不知道如何使用它。

server = grpc.server(futures.ThreadPoolExecutor(max_workers=100), options=[???])

来自gRPC documentation

options – 用于配置通道的键值对(gRPC 运行时中的通道参数)的可选列表。

如何创建这些选项?它们是字符串-字符串对吗?

不过,我是 Python 和 gRPC 的新手。

【问题讨论】:

    标签: python grpc


    【解决方案1】:

    您可以在这个 github 问题中找到一个示例:https://github.com/grpc/grpc/issues/11299

    对于 30mb 最大消息长度,请使用:

    options = [('grpc.max_message_length', 30 * 1024 * 1024)]

    【讨论】:

      【解决方案2】:

      如果您想将邮件大小从默认的 4MB 扩大到 30MB。 请参考这个How to increase message size in grpc using python

      options = [('grpc. max_receive_message_length', 30 * 1024 * 1024)]

      【讨论】:

        【解决方案3】:

        选项应该是Tuple[str, Any] 的列表。

        下面我添加一个更完整的服务器示例。虽然问题主要是关于要传递的 gRPC 选项的结构(之前已经回答过),但答案是链接到客户端实现的示例(因此通道调用可能有点混乱)。

        import grpc
        from concurrent import futures
        
        ...
        
        MAX_MESSAGE_LENGTH = 1024 * 1024 * 32
        
        ...
        
        _server = grpc.server(
            futures.ThreadPoolExecutor(max_workers=4),
            options=[("grpc.max_receive_message_length", MAX_MESSAGE_LENGTH)],
        )
        
        ... # add services to server
        
        _server.add_insecure_port("[::]:50051")
        _server.start()
        

        客户端在创建频道时设置选项(grpc.insecure_channelgrpc.secure_channel)。

        服务器端在创建服务器时设置选项 (grpc.server)。

        【讨论】:

          猜你喜欢
          • 2020-05-11
          • 1970-01-01
          • 2017-04-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-03
          • 1970-01-01
          相关资源
          最近更新 更多