In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces Python how to build gRPC service, the article is very detailed, has a certain reference value, interested friends must read it!
1. Install the libraries required by python.
Pip install grpciopip install grpcio-tools pip install protobuf
2. Define the gRPC interface.
Syntax = "proto3"; option cc_generic_services = true;// defines service interface service GrpcService {rpc hello (HelloRequest) returns (HelloResponse) {} / / multiple interfaces can be defined in a service, that is, multiple function functions} / / the requested parameter message HelloRequest {string data = 1; / / the number 1 is the position order of the parameter, not the value Skill skill = 2 to the parameter / / supports custom data format, which is very flexible}; / / the returned object message HelloResponse {string result = 1; map map_result = 2; / / supports map data format, similar to dict}; message Skill {string name = 1;}
3. Compile and generate the code of the corresponding language with protoc and corresponding plug-ins.
Python-m grpc_tools.protoc-I. /-- python_out=./-- grpc_python_out=. . / hello.proto
Use the compilation tool to convert the proto file into a py file, and run the above code directly in the current file directory.
4. Write grpc server code.
#! / usr/bin/env python# coding=utf8import timefrom concurrent import futuresimport grpcfrom gRPC_example import hello_pb2_grpc, hello_pb2_ONE_DAY_IN_SECONDS = 60 * 60 * 24class TestService (hello_pb2_grpc.GrpcServiceServicer):''inherit GrpcServiceServicer and implement hello method' 'def _ _ init__ (self): pass def hello (self, request, context):' 'concrete method to implement hello And construct HelloResponse according to the returned object of pb: param request:: param context:: return:''result= request.data + request.skill.name + "this is gprc test service" list_result = {"12": 1232} return hello_pb2.HelloResponse (result=str (result)) Map_result=list_result) def run ():''Simulation service startup: return:' 'server = grpc.server (futures.ThreadPoolExecutor (max_workers=10)) hello_pb2_grpc.add_GrpcServiceServicer_to_server (TestService (), server) server.add_insecure_port (' [::]: 50052') server.start () print ("start service...") Try: while True: time.sleep (_ ONE_DAY_IN_SECONDS) except KeyboardInterrupt: server.stop (0) if _ _ name__ ='_ _ main__': run ()
5. Write gRPC client code.
#! / usr/bin/env python# coding=utf8import grpcfrom gRPC_example import #! / usr/bin/env python# coding=utf8import grpcfrom gRPC_example import hello_pb2_grpc Hello_pb2def run ():''Simulation request service method information: return:' 'conn=grpc.insecure_channel (' localhost:50052') client = hello_pb2_grpc.GrpcServiceStub (channel=conn) skill= hello_pb2.Skill (name= "engineer") request = hello_pb2.HelloRequest (data= "xiao gang", skill=skill) respnse = client.hello (request) print ("received:" Respnse.result) if _ _ name__ = ='_ main__': run () def run ():''Simulation request service method information: return:' 'conn=grpc.insecure_channel (' localhost:50052') client = hello_pb2_grpc.GrpcServiceStub (channel=conn) skill = hello_pb2.Skill (name= "engineer") request = hello_pb2.HelloRequest (data= "xiao gang") Skill=skill) response = client.hello (request) print ("received:", response.result) if _ _ name__ = ='_ main__': run ()
6. Call the test.
First start the code that runs the server, and then start the code that runs the client.
The above is all the contents of the article "how to build gRPC Services for Python". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.