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.
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)]
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)]
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
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With