Python_Reference.webp

To work with gRPC in Python, you need to install the following packages:

sudo python3 -m pip install grpcio

python3 -m pip install grpcio-tools

Then you can automatically generate Python code with that command:

python3 -m grpc_tools.protoc -I./protos --python_out=. --pyi_out=. --grpc_python_out=. ./protos/prc.proto

This gets you the following three files:

prc_pb2_grpc.py

prc_pb2.py

prc_pb2.pyi

Along in the folder is the prc_localhost.pem certificate for the SSL encryption. It is included in the example project.

image.png

Import the following libraries for the sample

import logging
import os
import time
import threading
import grpc
import prc_pb2
import prc_pb2_grpc

Next, set up the path to the *.pem certificate and establish the GRPC channel. To avoid issues in the future, set the message lengths to unlimited. Also enable compression. Note that depending on the version of the grpc library, the compression may be grpc.Compression.Gzip instead of grpc._compression.Gzip.