Package: easy-grpc
Easy GRPCPython (>=3.7) GRPC service helper library.InstallationWe suggest to create a virtual environment.python3.7-mvenv./venvsourcevenv/bin/activate
python-mpipinstall--upgradepipInstall the module:pipinstalleasy-grpcUsage exampleEasy GRPC helps you to create a GRPC Service in three steps:Define and compile your proto filesImplement theActionsConfigure and run the GRPC service1. Define and compile your proto filesIn the example folder there are some files extracted from thegrpclibrepository, which is currently used to run this service. You can find thehelloworld.protoand the relative compiled python files (helloworld_pb2.pyandhelloworld_grpc.py).syntax="proto3";packageexample.helloworld;messageHelloRequest{stringname=1;}messageHelloReply{stringmessage=1;}serviceGreeter{rpcSayHello(HelloRequest)returns(HelloReply){}}To define the service you have to create the proto files as explained in the official documentationsgrpc.io.To compile a proto file by yourself, just execute this command from the root directory.python-mgrpc_tools.protoc\--proto_path=.\--python_out=.\--python_grpc_out=.\./example/helloworld.protoNote:pay attention to thepython_grpc_outparam. This is not standard in thegrpc_tools.protoclibrary, it isgrpclib specific.2. Implement theActionsIn the helloworld.proto file there is declared one rpc function (you can declare more). You shall create an action for each rpc, handling the defined input message (HelloRequest) and returning the relative return message (HelloReply).In the case of the example the helloworld_action.py is created:fromeasygrpcimportActionfrom.helloworld_pb2importHelloReplyclassHello(Action):asyncdefexecute(self,hello_request=None):returnHelloReply(message=f'Hello{hello_request.name}!')AnActionis a Class extending theeasygrpc.action.Actionabstract base class, and must overwrite the execute method.3. Configure and run the GRPC serviceFinally to run the service you shall create a config file which is used to configure and run the service.[SERVER]server=example.helloworld_grpc.GreeterBaseclient=example.client.SendRequesthost=127.0.0.1port=50051[ACTIONS]SayHello=example.helloworld_action.HelloIn theACTIONSsection you shall declare one action for each rcp function. Of corse you can declare more then the defined rpc that can be used and exposed.To run the service just open a terminal, and:sourcevenv/bin/activate
python-measygrpc.startTo execute the client, open another teminal, and:sourcevenv/bin/activate
python-measygrpc.start-cYou should see the response from the service:HelloMr.Easy!Database interactionIn every implemented Action you have access to a PostgreSQL Database Client Library (asyncpg). In the config file, you define the PostgreSQL connection parameters. And the in the action implementation you can execute SQL commands to interact with the database.create a postgreSQL (versions 9.2 to 10) database named 'easy'.sudo-upostgrescreatedb-EUTF8easyThen create a new table:CREATETABLEpublic.messages(idserial,textcharactervarying,PRIMARYKEY(id));And insert a row:INSERTINTOpublic.messages(text)VALUES('Hello PostgreSQL!');Add the following configuration section:[POSTGRESQL]user=postgrespassword=postgresdatabase=easyhost=localhostport=5432Modify the Hello Action (example/helloword_action.py):fromeasygrpcimportActionfrom.helloworld_pb2importHelloReplyclassHello(Action):asyncdefexecute(self,hello_request=None):rec=awaitself.conn.fetchval("""SELECT row_to_json(t)FROM (SELECTtext as messageFROM public.messagesWHEREid = $1) as t""",1)ifrecisnotNone:returnself.encode(rec,HelloReply)returnNoneTo run the service just open a terminal, and:sourcevenv/bin/activate
python-measygrpc.startTo execute the client, open another teminal, and:sourcevenv/bin/activate
python-measygrpc.start-cExample with multiple ServicesYou can of course declare more then one service and clients in your proto file:syntax="proto3";packageexample.helloworld;messageHelloRequest{stringname=1;}messageHelloReply{stringmessage=1;}serviceGreeter{rpcSayHello(HelloRequest)returns(HelloReply){}}serviceGreeterDB{rpcSayHello(HelloRequest)returns(HelloReply){}}In this case theconfig.inifile will be a little bit different:[POSTGRESQL]user=postgrespassword=postgresdatabase=easyhost=localhostport=5432[SERVER]server=example.helloworld_grpc.Greeter,example.helloworld_grpc.GreeterDBclient=example.client.SendRequest,example.client.SendRequestDbhost=127.0.0.1port=50051[ACTIONS]Greeter.SayHello=example.helloworld_action.HelloGreeterDB.SayHello=example.helloworld_action.HelloDbPay attention to theACTIONSsection. The keys are defined using a dot notation to identify which server is used with the given action.The clients can also be more than one (see ./example/client.py):importasynciofromgrpclib.clientimportChannel# generated by protocfrom.helloworld_pb2importHelloRequest,HelloReplyfrom.helloworld_grpcimportGreeterStub,GreeterDBStubasyncdefSendRequest(channel):greeter=GreeterStub(channel)reply=awaitgreeter.SayHello(HelloRequest(name='Mr. Easy'))print(reply.message)channel.close()asyncdefSendRequestDb(channel):greeter=GreeterDBStub(channel)reply=awaitgreeter.SayHello(HelloRequest(name='Mr. PostgreSQL'))print(reply.message)channel.close()To run the service is just same as starting a single service instance:sourcevenv/bin/activate
python-measygrpc.startTo execute the client, instead, you can chose which client to run:sourcevenv/bin/activate
python-measygrpc.start-cexample.client.SendRequestDbNote:If the client is not give as a the parameter than the first one is executed