快速入门

本指南通过一个简单的示例,帮助您开始在 Python 中使用 gRPC。

快速入门

本指南通过一个简单的示例,帮助您开始在 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.66.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 目录中

  1. 运行服务器

    python greeter_server.py
    
  2. 从另一个终端,运行客户端

    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 目录中

  1. 运行服务器

    python greeter_server.py
    
  2. 从另一个终端,运行客户端

    python greeter_client.py
    

下一步是什么

上次修改时间:2024 年 11 月 25 日:feat: move the $ shell line indicator to scss (#1354) (ab8b3af)