6

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.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Viacheslav Shalamov
  • 4,149
  • 6
  • 44
  • 66

3 Answers3

6

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

evaleria
  • 1,281
  • 6
  • 23
  • 30
1

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

Evan Lin
  • 1,272
  • 1
  • 12
  • 25
1

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

jso
  • 484
  • 5
  • 13