In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to use .proto file". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Back when I was young, when I was working on a project, I needed to calculate the value of item n in the Fibonacci series. But I will only use recursion to achieve it. As we all know, the recursive algorithm for calculating Fibonacci series is extremely inefficient and slow.
So I turned to my master at that time and asked him if he could help me solve this problem.
My master said, "Yes, I have, but the code is written in C++, and you probably won't understand it. But it doesn't matter, you can call it directly with Python."
I was surprised: "do you use Python to call C++ code directly? it seems troublesome."
Master said, "it's no trouble at all. I'll give you a .proto file and an address, and you can call it with the auto-generated code."
So, I got a mentors_secret.proto file that was very simple:
Syntax = "proto3"; message NumToCalc {int32 num = 1;} message Result {int32 result = 1; string msg = 2; bool success = 3;} service MentorsSecret {rpc CalcFib (NumToCalc) returns (Result) {}}
There is also an address: 122.51.39.219virtual 8766.
It turns out to be using gRPC. So I know what to do.
Installation environment
First, let's install the Python version of gRPC:
Pip install grpcio grpcio-tools
Generate code
Next, the code is generated automatically based on the mentors_secret.proto file. Cd enters the file where the mentors_secret.proto file is located and executes the following command:
Python3-m grpc_tools.protoc-I. -- python_out=. -- grpc_python_out=. . / mentors_secret.proto
At this point, you can see that two files are generated in the current folder: mentors_secret_pb2.py and mentors_secret_pb2_grpc.py.
As shown in the following figure:
There is no need to read the contents of these two documents.
Invoke remote service
Next, create a new file, called client.py, to call the remote function:
Import grpc from mentors_secret_pb2 import NumToCalc from mentors_secret_pb2_grpc import MentorsSecretStub channel = grpc.insecure_channel ('122.51.39.219) stub = MentorsSecretStub (channel) result = stub.CalcFib (NumToCalc (num=36)) print (' Fibonacci series 36 like:', result.result)
There are only 10 lines of code plus blank lines. 1-3 lines import module, 6 lines 7 lines to create remote links. Line 9 calls the remote function. Line 10 prints the result.
Let's see how it works:
The running result is out in seconds.
Summary
When we get a .proto file and need to invoke the gRPC service, we usually take the following steps:
(1) automatically generate mentors_secret_pb2.py and mentors_secret_pb2_grpc.py files.
(2) check the name MentorsSecret after service in the .proto file, as shown below:
(3) write fixed code:
Import grpc from mentors_secret_pb2_grpc import MentorsSecretStub # service name followed by Stub channel = grpc.insecure_channel ('remote service address and port') stub = MentorsSecretStub (channel)
(4) call remote functions. From the .proto file, you can see that the remote function CalcFib receives a parameter NumToCalc, so import it into: from mentors_secret_pb2 import NumToCalc, whose parameter is num, so assign a value when calling the remote service:
Para = NumToCalc (num=36) calc_result = stub.CalcFib (para)
(5) know from .proto that the returned result is the result attribute in Result. So print the calc_result.result to get the result.
That's all for "how to use the .proto file". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.