当前位置: 首页 > 工具软件 > python-grpc > 使用案例 >

Python-grpc快速入门

宋烨烁
2023-12-01

环境搭建

基础环境要求

  • Python 3.5 及以上
  • pip 版本高于 9.0.1

gRPC 安装

$ python -m pip install grpcio==1.38.0 grpcio-status==1.38.0 grpcio-tools==1.38.0 protobuf3-to-dict==0.1.5

参考自GreenPill

gRPC tools

这个是用来生成 client & server stub 内容的 Python 文件,stub 暂时先放放。

安装 gRPC tools:

$ python -m pip install grpcio-tools

安装的时候可能会碰到一个错误提示:

command '/usr/bin/clang' failed with exit code 1

这个时候我们可以以如下方式进行安装:

GRPC_PYTHON_BUILD_SYSTEM_OPENSSL=true GRPC_PYTHON_BUILD_SYSTEM_ZLIB=true pip install google-cloud-pubsub==2.1.0
# 参考文档:
# https://stackoverflow.com/questions/64881510/pip-install-on-mac-os-gets-error-command-usr-bin-clang-failed-with-exit-code

下载示例代码

我们通过 git 来对示例代码进行获取:

# Clone the repository to get the example code:
$ git clone -b v1.35.0 https://github.com/grpc/grpc
# Navigate to the "hello, world" Python example:
$ cd grpc/examples/python/helloworld

正式篇章

小试牛刀

首先进入到 examples/python/helloworld 这个目录下

1、 运行服务器

$ python greeter_server.py

2、 打开另一个窗口,运行客户端

$ python greeter_client.py

这个时候,客户端的 DOS 窗口会打印出一句话。

恭喜,完成!

对示例 proto文件 进行修改

此时我们可以让 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 对应的 Python代码文件:
此时我们切换回 examples/python/helloworld这个目录:

$ python -m grpc_tools.protoc -I../../protos --python_out=. --grpc_python_out=. ../../protos/helloworld.proto

命令行参数讲解

python -m grpc_tools.protoc -I$SRC_DIR --python_out=$DST_DIR --grpc_python_out=$DST_DIR  $SRC_DIR/helloworld.proto

参数说明:

  • $SRC_DIR:.proto 定义proto文件所在文件夹内部 的位置,如果不提供将使用当前路径。如果有多个路径,则重复调用 -I,并按序查找。
  • 如果是当前目录下:-I./
  • $DST_DIR:生成的代码希望放到哪。由于本例基于 Python,因此使用 --python_out,其他语言类似。
    • –python_out :xxx_pb2.py 文件的位置。
    • –grpc_python_out:xxx_pb2_grpc 文件的位置。
  • 最后是指向 .proto 文件的路径。

这行命令将生成一个 helloworld_pb2.py 及 helloworld_pb2_grpc.py 文件。

通过内置方法生成 proto 的 .py 文件

如果我们不想生成对应的 helloworld_pb2.py 及 helloworld_pb2_grpc.py 文件,也可以通过下面的方式来进行慢加载:

protos = grpc.protos("helloworld.proto")
services = grpc.services("helloworld.proto")
  • 返回的 protos 变量指的是 helloworld_pb2
  • 返回的 services 变量指的是 helloworld_pb2_grpc

除非玩的特别溜,否则不推荐使用。

更新 server 端 代码

此时我们打开greeter_server.py文件,参照 SayHello方法再重新书写一个方法:

class Greeter(helloworld_pb2_grpc.GreeterServicer):

    def SayHello(self, request, context):
    return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)

    def SayHelloAgain(self, request, context):
    return helloworld_pb2.HelloReply(message='Hello again, %s!' % request.name)
...

更新 Client 端代码

这里有关 channel 和 stub 的相关概念我们在基础手册中将会介绍

def run():
    channel = grpc.insecure_channel('localhost:50051')
    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)

Client 端打印输出:

Greeter client received: Hello, you!
Greeter client received: Hello, me!

参考文章:

GreenPill

下一篇:Python-grpc 基础手册

 类似资料: