快速入门
本指南通过一个简单的运行示例,帮助您开始使用 Python 中的 gRPC。
快速入门
先决条件
- Python 3.7 或更高版本
pip
9.0.1 或更高版本
如有必要,请升级您的 pip
版本
python -m pip install --upgrade pip
如果由于系统安装限制无法升级 pip
,您可以在 virtualenv 中运行示例
python -m pip install virtualenv
virtualenv venv
source venv/bin/activate
python -m pip install --upgrade pip
gRPC
安装 gRPC
python -m pip install grpcio
或者,全局安装
sudo python -m pip install grpcio
gRPC 工具
Python 的 gRPC 工具包含协议缓冲区编译器 protoc
和用于从 .proto
服务定义生成服务器和客户端代码的特殊插件。对于我们快速入门示例的第一部分,我们已经从 helloworld.proto 生成了服务器和客户端存根,但您需要这些工具才能完成快速入门的其余部分,以及后续教程和您自己的项目。
要安装 gRPC 工具,请运行
python -m pip install grpcio-tools
下载示例
您需要一份示例代码的本地副本才能完成本快速入门。从我们的 GitHub 仓库下载示例代码(以下命令将克隆整个仓库,但您只需快速入门和其他教程的示例即可)
# Clone the repository to get the example code:
git clone -b v1.74.0 --depth 1 --shallow-submodules https://github.com/grpc/grpc
# Navigate to the "hello, world" Python example:
cd grpc/examples/python/helloworld
运行 gRPC 应用程序
在 examples/python/helloworld
目录中
运行服务器
python greeter_server.py
在另一个终端中,运行客户端
python greeter_client.py
恭喜!您刚刚运行了一个使用 gRPC 的客户端-服务器应用程序。
更新 gRPC 服务
现在,我们来看看如何在服务器上添加一个额外的方法,供客户端调用以更新应用程序。我们的 gRPC 服务是使用协议缓冲区定义的;您可以在gRPC 简介和基础教程中找到更多关于如何在 .proto
文件中定义服务的信息。目前您只需要知道,服务器和客户端的“存根”都有一个 SayHello
RPC 方法,它从客户端接收 HelloRequest
参数并从服务器返回 HelloReply
,并且该方法定义如下:
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
让我们更新它,使 Greeter
服务有两个方法。编辑 examples/protos/helloworld.proto
并使用一个新的 SayHelloAgain
方法进行更新,该方法具有相同的请求和响应类型
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
// Sends another greeting
rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
记得保存文件!
生成 gRPC 代码
接下来,我们需要更新应用程序使用的 gRPC 代码,以使用新的服务定义。
在 examples/python/helloworld
目录中,运行
python -m grpc_tools.protoc -I../../protos --python_out=. --pyi_out=. --grpc_python_out=. ../../protos/helloworld.proto
这会重新生成 helloworld_pb2.py
(其中包含我们生成的请求和响应类)和 helloworld_pb2_grpc.py
(其中包含我们生成的客户端和服务器类)。
更新并运行应用程序
我们现在有了新生成的服务器和客户端代码,但我们仍然需要在示例应用程序的手写部分实现并调用新方法。
更新服务器
在同一目录中,打开 greeter_server.py
。像这样实现新方法:
class Greeter(helloworld_pb2_grpc.GreeterServicer):
def SayHello(self, request, context):
return helloworld_pb2.HelloReply(message=f"Hello, {request.name}!")
def SayHelloAgain(self, request, context):
return helloworld_pb2.HelloReply(message=f"Hello again, {request.name}!")
...
更新客户端
在同一目录中,打开 greeter_client.py
。像这样调用新方法:
def run():
with grpc.insecure_channel('localhost:50051') as channel:
stub = helloworld_pb2_grpc.GreeterStub(channel)
response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
print("Greeter client received: " + response.message)
response = stub.SayHelloAgain(helloworld_pb2.HelloRequest(name='you'))
print("Greeter client received: " + response.message)
运行!
和之前一样,在 examples/python/helloworld
目录中
运行服务器
python greeter_server.py
在另一个终端中,运行客户端
python greeter_client.py