Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Follow publication

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.

gRPC

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.

Protobuf Working

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.

Sample .proto file

API Types in gRPC

Types of API in gRPC

gRPC vs REST

gRPC vs REST

Getting Started

ClickKart

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

Methods

1. Setup- Installations and Downloads

2. Code

  • .env
.env file
  • product.proto
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
server.py
  • client.py
client.py

3. Testing

Run the server and client code in the seperate terminals.

python3 server.py

python3 client.py
Create Product — Request Product , Response ProductId
Get Product — Request ProductId , Response Product
Update Product — Request Product, Response Empty
Delete Product — Request ProductId, Response Empty
List Products — Request Empty, Response stream of products

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
Create Product — Request Product , Response ProductId
Get Product — Request ProductId , Response Product
Update Product — Request Product, Response Empty
Delete Product — Request ProductId, Response Empty
List Products — Request Empty, Response stream of products

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!

Github Website Linkedin

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Responses (3)

Write a response