First glimpse into gRPC through Python (Part 1)
Ivan Yu
Posted on January 12, 2023
1. What is gRPC and the purpose of it?
RPC (Remote Procedure Call) is a protocol that allows two programs to communicate with each other, those programs can locate in the same computer or in different computers.
gRPC(Google Remote Procedure Call) is a modern open source high performance Remote Procedure Call (RPC) framework, which is typically used in development of microserivces.
gRPC can be implmented by various programming languages, and in this blog, we will give the examples in Python.
2. Why do we learn it?
Large backend system development is shifting from monolith to microservice architecture. gRPC is one of the common ways to implement the microservice architecture.
Based on the graph below, there is more than 1 millions daily downloads for the package grpcio, which is used by Python for gRPC development. (Python alone!!😱)
Ref: Pypl Stats: grpcio download rate
3. Difference between gRPC and REST
You may say that we have REST to handle the communication between services, why do we need to learn one more new framework to waste our time?🤔
Ref: gRPC vs REST: comparing APIs architectural styles
I would like to highlight few points:
-
If data transmission speed plays an important role in the consideration of choosing framework, gRPC should be selected instead of REST.
gRPC used Protocol Buffer (binary data) for data transmisstion, which is more light-weight and faster compared to JSON or XML used by REST
If streaming communication matters to your project, you should think about gRPC
Note that gRPC has a limited support for browser and it may not be suitable to be used as a API open to the world. gRPC is mainly used for internal/private system.
4. Development Process Explanaiton
Development process of gRPC is as follows:
- Create a proto file and define function, input and output in it
- Generate pb2 .py and pb2_grpc .py files from the proto file by command
- Create a service .py to implement the Servicer interface in pb2_grpc .py
- Start service in the main function in service .py
- If there is any update in proto file, you MUST genearte pb2 .py and pb2_grpc .py files again
- Most of the use case is to use proto file to create a client object to send request to server. If the client is the same language as the server, then client can use Stub class in pb2_grpc .py file to send request.
For detail implementation steps, please refer to the Example Code, Quick Start and Python Basic Tutorial
5. Related Post & Github Link
Posted on January 12, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.