Difference between `protoc` and `python -m grpc_tools.protoc`
Asked Answered
O

1

10

To compile proto files for Python, I could

protoc -I=.--python_out=$DST_DIR sommem.proto

based on https://developers.google.com/protocol-buffers/docs/pythontutorial

or

python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. some.proto

based on https://grpc.io/docs/languages/python/basics/#generating-client-and-server-code

I wonder what's the difference between protoc and python -m grpc_tools.protoc, which one is more recommended for generating python *_pb2.py[i] files?

BTW, it looks protoc doesn't support --grpc_python_out.

Outthink answered 30/6, 2020 at 2:22 Comment(0)
H
12

protoc contains just logic for protocol buffers. That is, it will generate serialization/deserialization code for many languages. It does not, however, generate code for stubs and servers by default. This is left up to separate RPC systems through a system called protoc plugins.

Protoc plugins offer a simple interface by which an executable takes a description of a Protocol Buffer on stdin and outputs the corresponding generated code on stdout. Internal to Google, this system is used to generate code for Stubby. Externally, it is used to generate code for gRPC (or any other RPC system that wants to use protocol buffers).

Plugins get to register a command line flag for themselves to indicate where protoc should output the generated code. So, in your example above, --python_out indicates where the generated serialization/deserialization code should go, while --grpc_python_out is the flag registered by the gRPC Python code generator, indicating where Python stub and server code should be placed on the filesystem.

grpc_tools is a C extension bundling both protoc and the gRPC Python protoc plugin together so that the user doesn't have to deal with downloading protoc, downloading the gRPC Python code generator, and getting the necessary configuration set up to make them work together properly. However, in theory, you should be able to put all these pieces together to make them work just like grpc_tools (though I haven't tried).

Hawley answered 6/7, 2020 at 17:14 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.