Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify server options?

Tags:

python

grpc

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.

like image 643
Viacheslav Shalamov Avatar asked Jan 09 '18 14:01

Viacheslav Shalamov


3 Answers

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

like image 83
evaleria Avatar answered Oct 22 '22 19:10

evaleria


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

like image 35
Evan Lin Avatar answered Oct 22 '22 19:10

Evan Lin


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

like image 1
jso Avatar answered Oct 22 '22 19:10

jso