Skip to main content

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.

import greeter_pb2
import greeter_pb2_grpc
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


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.4.tar.gz (17.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_fast_grpc-0.3.4-py3-none-any.whl (20.0 kB view details)

Uploaded Python 3

File details

Details for the file python_fast_grpc-0.3.4.tar.gz.

File metadata

  • Download URL: python_fast_grpc-0.3.4.tar.gz
  • Upload date:
  • Size: 17.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.9.6 Darwin/24.6.0

File hashes

Hashes for python_fast_grpc-0.3.4.tar.gz
Algorithm Hash digest
SHA256 f3f0530fc9614300df0caa71af826cefd953cc957aa850e7acfaef692efae129
MD5 f2a7c62a70f79ab1a1b820f362bb6ba3
BLAKE2b-256 59c6e14c19520af71df830e56b5215190bc91dc569bff79696c4db4ae8fdc059

See more details on using hashes here.

File details

Details for the file python_fast_grpc-0.3.4-py3-none-any.whl.

File metadata

  • Download URL: python_fast_grpc-0.3.4-py3-none-any.whl
  • Upload date:
  • Size: 20.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.9.6 Darwin/24.6.0

File hashes

Hashes for python_fast_grpc-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 5efcf4453fa764047c44565072eed0333a19d7f8ff07933112f4c0a1988c5371
MD5 4d35588bdf456236c65a86c93be0bfc4
BLAKE2b-256 dd4f54d9bd3323002a092e9487d00da4670c2e55805f019b23a774141bdd9f0f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page