GRPC has been a subject of API development that I wanted to explore more, but I needed more time.

Until now

Until now

Motivation Link to heading

I wanted to jump into GRPC using Python, my language of comfort, to set up a sample project to go through the setup exercises of all the GRPC parts. For quickly getting up a modern HTTP API server, I use Fastapi, and for more season APIs, I use Django. I don’t have that for GRPC. The primary purpose of this post is to showcase setting up a simple GRPC that I can build on for future applications.

Repository Link to heading

https://github.com/darrylbalderas/helloworld-python-grpc

Steps Link to heading

Here is the site that I used to get started with my first GRPC project

  1. Run python -m pip install -r requirements.txt. It should install grpc and grpcio-tools dependencies so that we can get started with running GRPC server

  2. Copy grpc client example to greeter_client.py file

  3. Copy grpc server example to greeter_server.py file

  4. Copy grpc hello proto example into protos folder

  5. Run this command to generate python grpc code python -m grpc_tools.protoc -I protos/ --python_out=./protos --pyi_out=./protos --grpc_python_out=./protos protos/helloworld.proto

  6. Modiy import statements in greeter_server.py, greeter_client.py, and protos/helloworld_pb2_grpc.py to point to the correct Python proto buff generated code. For example;

    21import helloworld_pb2 as helloworld_pb2
    22import helloworld_pb2_grpc as helloworld_pb2_grpc

    Update to

    21import protos.helloworld_pb2 as helloworld_pb2
    22import protos.helloworld_pb2_grpc as helloworld_pb2_grpc
  7. Run server python greeter_server.py

  8. Run client python greeter_client.py

Expected output Link to heading

The Client should submit a request to the Server to SayHello.

Client output Link to heading

$: python greeter_client.py
Will try to greet world ...
Greeter client received: Hello, you!

Server output Link to heading

$: python greeter_server.py
Server started, listening on 50051

Learnings Link to heading

Quickly doing this small project showed me the capabilities of how GRPC can scale and solve problems that HTTP can struggles like

  • Handshake problem.
  • Streaming data
  • Agreeable data contract between the Client and Server