FastServe
FastServe is a Python library that lets you build gRPC services quickly and easily without hand-writing protobuf files.
It automatically generates protobuf files that match the method signature of the Python object provided when starting the server.
Then, it uses the grpcio-tool to generate modules include server and client stub classes from the created protobuf files.
By dynamically generating a subclass of the server stub class and associating its methods with the original Python object's methods, it builds the gRPC server.
Installation
Install it using your favorite package manager. Below is the method for installation using pip.
pip install fastserve
Usage
Here's an example of creating a dead simple greeter service.
greeting.py
from fastserve.core import Server, Message
class HelloRequest(Message):
name: str
class HelloReply(Message):
message: str
class Greeter:
def say_hello(self, request: HelloRequest) -> HelloReply:
return HelloReply(message=f"Hello, {request.name}!")
if __name__ == "__main__":
s = Server()
s.run(Greeter())
To start the server, run:
$ python greeting.py
gRPC server is running...
In another terminal, you can call the gRPC service:
$ grpcurl -plaintext -d '{"name": "World"}' localhost:50051 greeter.v1.Greeter/SayHello
{
"message": "Hello, World!"
}
To list the services, use:
$ grpcurl -plaintext localhost:50051 list
greeter.v1.Greeter
grpc.health.v1.Health
grpc.reflection.v1alpha.ServerReflection
By the way, running this will create greeter.proto, greeter_pb2.py, and greeter_pb2_grpc.py in the current directory.
The generated greeter.proto will look like this:
syntax = "proto3";
package greeter.v1;
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply);
}
message HelloReply {
string message = 1;
}
message HelloRequest {
string name = 1;
}
You can share the generated greeter.proto with other services/clients written in other programming languages or Python if you want or need.
If You Want to Use Existing Stub Modules
If you already have greeter_pb2.py and greeter_pb2_grpc.py and want to use them without generating, use mount_using_pb2_modules as shown below:
from fastserve.core import Server, Message
import greeter_pb2_grpc, greeter_pb2
class HelloRequest(Message):
name: str
class HelloReply(Message):
message: str
class Greeter:
def say_hello(self, request: HelloRequest) -> HelloReply:
return HelloReply(message=f"Hello, {request.name}!")
if __name__ == "__main__":
s = Server()
s.mount_using_pb2_modules(greeter_pb2_grpc, greeter_pb2, Greeter())
s.run()
asyncio Version
import asyncio
from fastserve.core import AsyncIOServer, Message
class HelloRequest(Message):
name: str
class HelloReply(Message):
message: str
class Greeter:
async def say_hello(self, request: HelloRequest) -> HelloReply:
return HelloReply(message=f"Hello, {request.name}!")
if __name__ == "__main__":
s = AsyncIOServer()
loop = asyncio.get_event_loop()
loop.run_until_complete(s.run(Greeter()))
If You Only Want to Generate protobuf Files and Stub Modules
You can execute the fastserve.core module to only generate the protobuf files and stub modules as follows:
$ python -m fastserve.core greeting.py Greeter
Doing so will create greeter.proto, greeter_pb2.py, and greeter_pb2_grpc.py in the current directory.
Note
- Yuo can serve multiple services by passing multiple objects to
Server.runor callingServer.mountmultiple times. - You can specify the port number with
Server.set_portmethod before callingrun. - You can set interceptors with
Serverconstructor'sinterceptorsargument.
Mapping of Python Types to Protobuf Types
The following table shows the mapping of Python types to protobuf types.
| Python Type | Protobuf Type |
|---|---|
int |
int32 |
float |
float |
str |
string |
bytes |
bytes |
bool |
bool |
List[T] |
repeated T |
Dict[str, T] |
map<string, T> |
Dict[int, T] |
map<int32, T> |
fastserve.core.Message |
message |
TODO
- Support for mapping of
types.UnionTypetooneof - Support for mapping of
typing.Optionalortyping.Union[T, None]tooneof - Support for mapping of
enum.Enumtoenum - Support for mapping of
datetime.datetimetogoogle.protobuf.Timestamp - Support for mapping of
datetime.timedeltatogoogle.protobuf.Duration
License
MIT License
Author
Yasushi Itoh (i2y)
Metadata
Release files for fastserve 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| fastserve-0.1.0.tar.gz | 10.4 kB | Without provenance | Details for fastserve-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| fastserve-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for fastserve-0.1.0-py3-none-any.whl |
Total release size: 18.9 kB
Release files / fastserve-0.1.0.tar.gz
| Download URL | fastserve-0.1.0.tar.gz |
|---|---|
| Size | 10.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a6fc635e1d64bc3f69b33378634cd9a1366572f04f9c7c0972e8663dc02b8aa0
|
|
BLAKE2b-256 checksum How to use checksums |
ef8469fcc699a9dbec27f9b2344f0b1e27d4e95024f7940a48f982df9ce4133b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.3
|
Release files / fastserve-0.1.0-py3-none-any.whl
| Download URL | fastserve-0.1.0-py3-none-any.whl |
|---|---|
| Size | 8.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
50b822c51afe0215cdb6e8b1a922474944dcea5a5dc7bcfd2721fdcba80a9f70
|
|
BLAKE2b-256 checksum How to use checksums |
75204b9781af52ca251ada4bbbf0dd5b35c301ba2267cc803b7b9e58dee3b3aa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.3
|
Security for fastserve 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.