First glimpse into gRPC through Python (Part 1)
Tags: python, grpc, microservices, restapi
Published: 2023-01-12 16:45:00


## 1. What is gRPC and the purpose of it?
[RPC](https://www.techtarget.com/searchapparchitecture/definition/Remote-Procedure-Call-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](https://grpc.io/)(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](https://grpc.io/docs/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](https://cdn.hashnode.com/res/hashnode/image/upload/v1655024626536/6pdzxy_lL.png)
Ref: [Pypl Stats: grpcio download rate](https://pypistats.org/packages/grpcio)

## 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](https://cdn.hashnode.com/res/hashnode/image/upload/v1657864487047/hIlhHKJtc.png)

Ref: [gRPC vs REST: comparing APIs architectural styles](https://www.imaginarycloud.com/blog/grpc-vs-rest/)

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](https://github.com/ivanyu199012/7.-gRPC-app), [Quick Start](https://grpc.io/docs/languages/python/quickstart/) and [Python Basic Tutorial](https://grpc.io/docs/languages/python/basics/)

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1657863476725/m-7dBq7Ud.png)

## 5. Related Post & Github Link
- [First glimpse into gRPC through Python (Part 2)](https://ivanyu2021.hashnode.dev/first-glimpse-into-grpc-through-python-part-2)
- [Example Code](https://github.com/ivanyu199012/7.-gRPC-app)