Add your description here
Project description
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)
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastserve-0.1.0.tar.gz.
File metadata
- Download URL: fastserve-0.1.0.tar.gz
- Upload date:
- Size: 10.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a6fc635e1d64bc3f69b33378634cd9a1366572f04f9c7c0972e8663dc02b8aa0
|
|
| MD5 |
07a373a67989194b76d6d88166cb8a04
|
|
| BLAKE2b-256 |
ef8469fcc699a9dbec27f9b2344f0b1e27d4e95024f7940a48f982df9ce4133b
|
File details
Details for the file fastserve-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastserve-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
50b822c51afe0215cdb6e8b1a922474944dcea5a5dc7bcfd2721fdcba80a9f70
|
|
| MD5 |
4a6ee35300ff2d9a34e57ac360ec2883
|
|
| BLAKE2b-256 |
75204b9781af52ca251ada4bbbf0dd5b35c301ba2267cc803b7b9e58dee3b3aa
|