Backend Development 9 min read

Getting Started with gRPC in Python: Concepts, Setup, and Example

This article introduces the high‑performance gRPC framework, explains why it uses HTTP/2 and Protocol Buffers, outlines its core components, and provides a step‑by‑step Python tutorial with environment setup, code generation, server and client implementations, and common application scenarios.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Getting Started with gRPC in Python: Concepts, Setup, and Example

What is gRPC?

gRPC is an open‑source, high‑performance RPC framework built on the HTTP/2 protocol and using Protocol Buffers for data serialization. It abstracts transport details (TCP/UDP, XML/JSON) so that callers can invoke remote services as if they were local functions.

Why HTTP/2?

HTTP/2 offers multiplexed streams, header compression, and better compatibility with modern proxies, making it ideal for gRPC’s low‑latency, high‑throughput requirements compared with HTTP/1.x.

Core Concepts

Uses http2 as the transport layer.

Uses protobuf for efficient serialization.

Code is generated from .proto files via the protoc plugin.

Quick Start Guide

1. Build the environment

<code># need python3.5+</code>
<code>$ python -m pip install virtualenv</code>
<code>$ virtualenv venv</code>
<code>$ source venv/bin/activate</code>
<code>$ python -m pip install --upgrade pip</code>

2. Install gRPC packages

<code>$ python -m pip install grpcio</code>
<code>$ python -m pip install grpcio-tools</code>

3. Write a .proto file

<code>syntax = "proto3";</code>
<code>package greeterpb;</code>
<code>service Greeter {</code>
<code>  rpc SayHello (HelloRequest) returns (HelloReply) {}</code>
<code>  rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}</code>
<code>}</code>
<code>message HelloRequest { string name = 1; }</code>
<code>message HelloReply { string message = 1; }</code>

4. Generate code

<code>$ python -m grpc_tools.protoc \</code>
<code>    --python_out=. --grpc_python_out=. -I. \</code>
<code>    helloworld.proto</code>

This creates helloworld_pb2.py and helloworld_pb2_grpc.py .

5. Implement the server

<code>from concurrent import futures</code>
<code>import time, grpc</code>
<code>import helloworld_pb2, helloworld_pb2_grpc</code>
<code>class Greeter(helloworld_pb2_grpc.GreeterServicer):</code>
<code>    def SayHello(self, request, context):</code>
<code>        return helloworld_pb2.HelloReply(message=f"hello {request.name}")</code>
<code>    def SayHelloAgain(self, request, context):</code>
<code>        return helloworld_pb2.HelloReply(message=f"hello {request.name}")</code>
<code>def serve():</code>
<code>    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))</code>
<code>    helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)</code>
<code>    server.add_insecure_port('[::]:50051')</code>
<code>    server.start()</code>
<code>    try:</code>
<code>        while True: time.sleep(60*60*24)</code>
<code>    except KeyboardInterrupt:</code>
<code>        server.stop(0)</code>
<code>if __name__ == '__main__':</code>
<code>    serve()</code>

6. Implement the client

<code>import grpc</code>
<code>import helloworld_pb2, helloworld_pb2_grpc</code>
<code>def run():</code>
<code>    channel = grpc.insecure_channel('localhost:50051')</code>
<code>    stub = helloworld_pb2_grpc.GreeterStub(channel)</code>
<code>    resp = stub.SayHello(helloworld_pb2.HelloRequest(name='czl'))</code>
<code>    print('Greeter client received: ' + resp.message)</code>
<code>    resp = stub.SayHelloAgain(helloworld_pb2.HelloRequest(name='daydaygo'))</code>
<code>    print('Greeter client received: ' + resp.message)</code>
<code>if __name__ == '__main__':</code>
<code>    run()</code>

7. Run the example

<code>$ python helloworld_grpc_server.py</code>
<code>$ python helloworld_grpc_client.py</code>

The guide also mentions updating the service by modifying the .proto file and regenerating code, and highlights typical gRPC application scenarios such as microservices and Kubernetes deployments.

microservicesbackend developmentgRPCHTTP/2Protocol Buffers
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.