gRPC Python implementation from scratch

Vikesh Yadav
3 min readMay 26, 2021

--

The objective of this article is to implement gRPC in Python. We will be implementing both client and server in your favorite programming language ie. Python :).

By the time you are reading this article, I am sure you have heard about gRPC and it’s working. But still here is a quick introduction to gRPC and its attributes.

💡What is gRPC?
gRPC is a very lightweight communication protocol from Google. At a high level, it allows you to define REQUEST and RESPONSE for RPC(Remote Procedure Calls) and handles for you.

gRPC is designed to make the client believe that the server is residing on the same machine as the client. gRPC client invokes the method on the client stub, which is handled by the PRC protocol.

gRPC is based on Protocol Buffers, an open-source Interface Definition Language and serialization library. It’s smaller, faster, and more efficient in terms of CPU consumption than other wired-format protocols.

💡 Why we should use gRPC?
gRPC works with many transports, by default it uses HTTP/2 but it can also be implemented on QUIC , Cronet, etc.

In HTTP/2 protocol client stub talks to the server stub on a single TCP connection called Channel. A Channel is a tunnel that is established between client and server stub once and the requests are multiplexed to be in flight without compromising network resources. The payload is converted to binary rather than text(in REST+JSON) that keeps the payload compact and efficient.

Note that the creation of the channel can take up to 3 seconds. If you have a deadline configured in the client and sent a request before the channel is established, the request will be dropped and RPC will be canceled, refer to this for a better understanding of the working of the channel.

  1. Create protocol buffers (proto)

The proto contains an API service that has a simple RPC where a client will send a request and wait for the response, the call will be just like a normal function call.

This is how your project should look like

.
├── client.py
├── protos
│ └── api.proto
├── __pycache__
├── README.md
└── server.py

2. Let’s create gRPC classes

Once you are done with the proto run the following command to let gRPC auto-generate code for you.

mkdir generated_code
python -m grpc_tools.protoc -I./protos --python_out=./generated_code --grpc_python_out=./generated_code ./protos/api.proto

Two new files (classes) will be generated inside the generated_code folder.

╰─➤  tree                                                                                                                                                              
├── api_pb2_grpc.py # contains server and client code
├── api_pb2.py # contains request and response code
├── client.py
├── protos
│ └── api.proto
├── README.md
└── server.py

One of these files contains Request and Response classes while the other contains Server and Client classes.

api_pb2_grpc.py — contains Server and Client classes
api_pb2.py — contains Request and Response classes

3. Server and Client class

Now that all the required piece of code is available, let's create server and client

To start the server run the following command

╰─➤  python server.py                                                                                                                                                    
server stub started on port 7073 ...

The client is going to send a username to receive greetings

╰─➤  python client.py                                                                                                          
resp: "Hello Vikesh"

This is a basic implementation of gRPC in python, we can use it to create micro-services that serve our cause but this is just to show how we start with it.

If you want me to implement any other feature, feel free to tell me by writing in the comment section.

The complete codes mentioned here can be found in this repo:

Happy Coding :)

--

--