Implementing gRPC In Python
This article aims to outline the basics of gRPC and create a simple project by building endpoints using gRPC.

What is gRPC ?
In gRPC (Google Remote Procedure Call), a client application can directly call a method on a server application on a different machine. It is based on the idea of defining services, specifying the methods that can be called remotely with their parameters and return types. On the server side, the server implements this interface and runs a gRPC server to handle client calls. It uses protocol buffers for communications, a mechanism for serializing data.

Protocol Buffers/ Protobuf
It is an Interface Definition Language(data format) used to serialize data (process of converting some in-memory object to a format that could be easily stored or sent over the network). Protobuf messages and services are described in the .proto
files.

gRPC relies on the protoc compiler to generate code. It uses a plugin to supplement the generated code by plain protoc with gRPC-specific code. For a .proto containing gRPC services, the plain protoc generated code is synthesized into a _pb2.py file, and the gRPC-specific code into a _pb2_grpc.py file.
API Types in gRPC

gRPC vs REST

Getting Started

We will create a simple project by building gRPC for managing Products for an ecommerce (ClickKart). Below are the endpoints to be implemented —

1. Setup- Installations and Downloads
- Download the couchbase server community version. Post download, create a new cluster with a new bucket and user. Give the bucket name and username of your choice and use the same names (bucketname , username and password) in .env file.
- Install grpc, grpcio-tools,couchbase, load_dotenv packages
- Download postman
2. Code
- .env
- product.proto
Now execute the below command. Post execution two files namely product_pb2.py and product_pb2_grpc.py should be generated.
python3 -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. protos/product.proto
- server.py
- client.py
3. Testing
Run the server and client code in the seperate terminals.
python3 server.py
python3 client.py





Testing in Postman
Postman supports calling gRPC services. Open the postman application and click on the New button and select gRPC. Paste the below text in the URL section :
localhost:5000
Import the product.proto file by clicking on the dropdown in the select the method text area. Post import all the service methods will be available in the method dropdown.
Now just run the server code by executing the below command in the terminal
python3 server.py





Conclusion
In this story, we have seen the basics of gRPC and learnt how to implement gRPC using python in simple steps. Hope everything is clear !
Thanks for reading!