Fast to Code gRPC in Python
Project description
FastGRPC
🚀 Build gRPC services in Python 3.9+ as easily as FastAPI.
Installation
Require Python 3.9+
pip install python-fast-grpc
A Simple Example
Define a gRPC service:
from pydantic import BaseModel
from fast_grpc import FastGRPC
app = FastGRPC()
class HelloRequest(BaseModel):
name: str
class HelloReply(BaseModel):
message: str
@app.unary_unary()
async def say_hello(request: HelloRequest) -> HelloReply:
return HelloReply(message=f"Greeter SayHello {request.name}")
if __name__ == '__main__':
app.run()
Client Test:
import grpc
import fast_grpc_pb2 as pb2
import fast_grpc_pb2_grpc as pb2_grpc
channel = grpc.insecure_channel("127.0.0.1:50051")
stub = pb2_grpc.FastGRPCStub(channel)
response = stub.SayHello(pb2.HelloRequest(name="FastGRPC"))
print("Client received: ", response)
Use Middleware
@app.middleware()
async def middleware(call_next, request, context):
print("before request")
response = await call_next(request, context)
print("after request")
return response
@app.middleware(is_server_streaming=True)
async def middleware(call_next, request, context):
print("before streaming request")
async for response in call_next(request, context):
yield response
print("after streaming request")
Service
Use Service for modular design, similar to FastAPI's router.
from fast_grpc import Service
srv = Service(name="Greeter")
@srv.unary_unary()
async def say_again(request: HelloRequest) -> HelloReply:
return HelloReply(message=f"Greeter SayHello {request.name}")
Pb2Service
Use Pb2Service if you're working with generated *_pb2.py and *_pb2_grpc.py files.
srv = Pb2Service("Greeter", pb2_module=greeter_pb2, pb2_grpc_module=greeter_pb2_grpc)
@srv.unary_unary()
async def say_again(request: HelloRequest) -> HelloReply:
return HelloReply(message=f"Greeter SayHello {request.name}")
Generate Clients Using Pydantic
Automatically generate a Pydantic-based gRPC client from .proto files:
from fast_grpc.proto import proto_to_python_client
proto_to_python_client("fast_grpc.proto")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
python_fast_grpc-0.3.0.tar.gz
(18.2 kB
view details)
Built Distribution
File details
Details for the file python_fast_grpc-0.3.0.tar.gz
.
File metadata
- Download URL: python_fast_grpc-0.3.0.tar.gz
- Upload date:
- Size: 18.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.6 Darwin/23.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
c8333d94bf475c2d54488810d239d782ecfcc15503df0cb85223a3390695f1b8
|
|
MD5 |
949c9bef0386ba70af2b1ea0819d238c
|
|
BLAKE2b-256 |
25dc491c1bce68ff8453505d667c2d9e692b6c7580f664cb8e3a63154e7e723d
|
File details
Details for the file python_fast_grpc-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: python_fast_grpc-0.3.0-py3-none-any.whl
- Upload date:
- Size: 20.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.6 Darwin/23.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
2376e51c2a4332c2abb3f193af4bcd6e3f7373784825850e7becd4faaea6af92
|
|
MD5 |
3e8b311a2fadaa922f0f0c4414c8e564
|
|
BLAKE2b-256 |
3f9a2153540545b0856b84ae0da30706c5c6faeda3dcdd8d706bb58e6b6669c2
|