Package: fastserve
FastServeFastServe 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.InstallationInstall it using your favorite package manager. Below is the method for installation using pip.pipinstallfastserveUsageHere's an example of creating a dead simple greeter service.greeting.pyfromfastserve.coreimportServer,MessageclassHelloRequest(Message):name:strclassHelloReply(Message):message:strclassGreeter:defsay_hello(self,request:HelloRequest)->HelloReply:returnHelloReply(message=f"Hello,{request.name}!")if__name__=="__main__":s=Server()s.run(Greeter())To start the server, run:$pythongreeting.pygRPC server is running...In another terminal, you can call the gRPC service:$grpcurl-plaintext-d'{"name": "World"}'localhost:50051greeter.v1.Greeter/SayHello{"message": "Hello, World!"}To list the services, use:$grpcurl-plaintextlocalhost:50051listgreeter.v1.Greetergrpc.health.v1.Healthgrpc.reflection.v1alpha.ServerReflectionBy the way, running this will creategreeter.proto,greeter_pb2.py, andgreeter_pb2_grpc.pyin the current directory.
The generatedgreeter.protowill look like this:syntax="proto3";packagegreeter.v1;serviceGreeter{rpcSayHello(HelloRequest)returns(HelloReply);}messageHelloReply{stringmessage=1;}messageHelloRequest{stringname=1;}You can share the generatedgreeter.protowith other services/clients written in other programming languages or Python if you want or need.If You Want to Use Existing Stub ModulesIf you already havegreeter_pb2.pyandgreeter_pb2_grpc.pyand want to use them without generating, usemount_using_pb2_modulesas shown below:fromfastserve.coreimportServer,Messageimportgreeter_pb2_grpc,greeter_pb2classHelloRequest(Message):name:strclassHelloReply(Message):message:strclassGreeter:defsay_hello(self,request:HelloRequest)->HelloReply:returnHelloReply(message=f"Hello,{request.name}!")if__name__=="__main__":s=Server()s.mount_using_pb2_modules(greeter_pb2_grpc,greeter_pb2,Greeter())s.run()asyncio Versionimportasynciofromfastserve.coreimportAsyncIOServer,MessageclassHelloRequest(Message):name:strclassHelloReply(Message):message:strclassGreeter:asyncdefsay_hello(self,request:HelloRequest)->HelloReply:returnHelloReply(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 ModulesYou can execute thefastserve.coremodule to only generate the protobuf files and stub modules as follows:$python-mfastserve.coregreeting.pyGreeterDoing so will creategreeter.proto,greeter_pb2.py, andgreeter_pb2_grpc.pyin the current directory.NoteYuo can serve multiple services by passing multiple objects toServer.runor callingServer.mountmultiple times.You can specify the port number withServer.set_portmethod before callingrun.You can set interceptors withServerconstructor'sinterceptorsargument.Mapping of Python Types to Protobuf TypesThe following table shows the mapping of Python types to protobuf types.Python TypeProtobuf Typeintint32floatfloatstrstringbytesbytesboolboolList[T]repeated TDict[str, T]map<string, T>Dict[int, T]map<int32, T>fastserve.core.MessagemessageTODOSupport for mapping oftypes.UnionTypetooneofSupport for mapping oftyping.Optionalortyping.Union[T, None]tooneofSupport for mapping ofenum.EnumtoenumSupport for mapping ofdatetime.datetimetogoogle.protobuf.TimestampSupport for mapping ofdatetime.timedeltatogoogle.protobuf.DurationLicenseMIT LicenseAuthorYasushi Itoh (i2y)