Quickstart
Build and run your first fastgrpc service.
Install
Requires Python 3.11 or later.
Write a service
Create main.py:
from dataclasses import dataclass
from fastgrpc import rpc, service
@dataclass
class User:
id: int
name: str
@service
class UserService:
@rpc
async def get_user(self, user_id: int) -> User:
return User(id=user_id, name="Ashesh")
That's the entire service. No .proto, no protoc, no servicer subclass.
Run it
You should see:
fastgrpc auto-detected async def, so it picked grpc.aio.server(). If you'd written def get_user(...) instead, it would have used the sync grpc.server thread-pool server.
Call it
In another terminal:
What just happened
When you ran fastgrpc dev, the framework:
- Imported
main.py— the@serviceand@rpcdecorators registeredUserServiceandget_user. - Inspected the type hints to derive a protobuf schema.
- Wrote
.fastgrpc/fastgrpc.protoand ranprotocto compile_pb2.pyand_pb2_grpc.pystubs. - Synthesized a
UserServiceImplsubclass at runtime that wraps yourget_userwith proto↔dataclass conversion. - Started a
grpc.aio.server()on port 50051 and registered the implementation. - Started a Rust-backed file watcher that triggers steps 1–5 again on every
.pychange.
Inspect the generated proto:
syntax = "proto3";
package fastgrpc;
message GetUserRequest {
int64 user_id = 1;
}
message User {
int64 id = 1;
string name = 2;
}
service UserService {
rpc GetUser (GetUserRequest) returns (User);
}
Next steps
- Watch mode — how reload works
- Async vs sync — when each server type is picked
- Streaming — server, client, and bidirectional streaming
- Lock file — why
.fastgrpc.lockmatters