How do I specify server options?

佐手、 提交于 2021-01-27 04:37:21

问题


I'm trying to run gRPC server in Python. I found a way to do it like this:

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

I need to add some options to the server like max_send_message_length, max_receive_message_length, etc. There is an options argument in grpc.server(...), but I can't figure out how to use it.

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

From gRPC documentation:

options – An optional list of key-value pairs (channel args in gRPC runtime) to configure the channel.

How do I create these options? Are they string-string pairs?

I'm new to Python and gRPC, though.


回答1:


You can find an example in this github issue: https://github.com/grpc/grpc/issues/11299

For 30mb max message length use:

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




回答2:


If you want to enlarge your message size from default 4MB to 30MB. Please refer this How to increase message size in grpc using python

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




回答3:


The options should be a list of Tuple[str, Any].

Below I add a a bit more complete server example. Though the question is mainly about the structure of the gRPC options to be passed (which was answered before already), the answers are linking to examples of client implementations (and therefore the channel calls might be a bit confusing).

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

Client side sets options when creating a channel (grpc.insecure_channel, grpc.secure_channel).

Server side sets options when creating a server (grpc.server).



来源:https://stackoverflow.com/questions/48170761/how-do-i-specify-server-options

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