First glimpse into gRPC through Python (Part 1)

A simple introduction of gRPC concept and development process using Python

ยท

3 min read

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!!๐Ÿ˜ฑ) image.png 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?๐Ÿค”

Compare gRPC and REST: image.png

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:

  1. Create a proto file and define function, input and output in it
  2. Generate pb2 .py and pb2_grpc .py files from the proto file by command
  3. Create a service .py to implement the Servicer interface in pb2_grpc .py
  4. Start service in the main function in service .py
  5. If there is any update in proto file, you MUST genearte pb2 .py and pb2_grpc .py files again
  6. 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

image.png

5. Related Post & Github Link

Did you find this article valuable?

Support Ivan Yu by becoming a sponsor. Any amount is appreciated!

ย