首页 > 编程语言 >python asyncio grpc

python asyncio grpc

时间:2024-08-11 12:27:15浏览次数:10  
标签:name pb2 python server grpc msg import asyncio

1. 准备环境

python3.11 -m venv venv
source venv/*/activate
pip install grpcio-tools #包含了grpcio和protobuf
pip install types-protobuf grpc-stubs # 可选安装,用于mypy静态检查

2. 编写msg.proto

syntax = "proto3";

// 这是注释,同时也是类文档
service MsgService {
 rpc handler (MsgRequest) returns (MsgResponse){}
}

// 这也是注释
message MsgRequest {
  // 1,2,3...是字段编号,正整数就行,可以不连续
  string name = 1;  // 姓名
  optional uint32 age = 2;  // 年龄
  optional float high = 3;  // 身高
  optional bytes avatar = 4;  // 头像
}


message MsgResponse {  // 注释也可以在行尾
  uint64 id = 1; // ID
  Role role = 2;  // 角色
  optional uint64 last_login = 10;  // 上一次登陆的时间戳
}

// 角色(嵌套字段)
message Role {
  string name = 1;
  int32 level = 2;
}

3. 把proto编译成python文件

python -m grpc_tools.protoc -I . --python_out=. --grpc_python_out=. msg.proto
ls msg_pb2*.py

4. 服务端程序msg_server.py

参考了官方示例:https://github.com/grpc/grpc/blob/master/examples/python/helloworld/async_greeter_server_with_graceful_shutdown.py

#!/usr/bin/env python3
import asyncio

import grpc

import msg_pb2
import msg_pb2_grpc

try:
    from rich import print
except ImportError:
    ...


class MsgServicer(msg_pb2_grpc.MsgServiceServicer):
    def handler(self, request: "msg_pb2.MsgRequest", context) -> "msg_pb2.MsgResponse":
        print("Received name: %s" % request.name)
        # 响应的处理逻辑写在这里
        # ...
        role = {'name': request.name, 'level': 0}
        return msg_pb2.MsgResponse(role=role, id=1)


def serve() -> None:
    _cleanup_coroutines = []

    async def run() -> None:
        server = grpc.aio.server()
        msg_pb2_grpc.add_MsgServiceServicer_to_server(MsgServicer(), server)
        listen_addr = "[::]:50051"
        server.add_insecure_port(listen_addr)
        print(f"Starting server on {listen_addr}")
        await server.start()

        async def server_graceful_shutdown():
            print("Starting graceful shutdown...")
            # Shuts down the server with 5 seconds of grace period. During the
            # grace period, the server won't accept new connections and allow
            # existing RPCs to continue within the grace period.
            await server.stop(5)
            print(f"{server} was graceful shutdown~")

        _cleanup_coroutines.append(server_graceful_shutdown())
        await server.wait_for_termination()

    loop = asyncio.get_event_loop()
    try:
        loop.run_until_complete(run())
    finally:
        loop.run_until_complete(*_cleanup_coroutines)
        loop.close()


if __name__ == "__main__":
    serve()

5. 启动服务

python msg_server.py
# Starting server on [::]:50051

6. 客户端代码msg_client.py

import asyncio
import os

import grpc

import msg_pb2
import msg_pb2_grpc

try:
    from rich import print
except ImportError:
    ...


def main():
    async def run() -> None:
        host = os.getenv("RPC_HOST", "localhost")
        async with grpc.aio.insecure_channel(f"{host}:50051") as channel:
            stub = msg_pb2_grpc.MsgServiceStub(channel)

            response = await stub.handler(msg_pb2.MsgRequest(name="you"))
            print("Client received: ")
            print(response)

    asyncio.run(run())


if __name__ == "__main__":
    main()

7. 运行客户端

python msg_client.py

结果如下:

Client received:
id: 1
role {
  name: "you"
}

标签:name,pb2,python,server,grpc,msg,import,asyncio
From: https://www.cnblogs.com/waketzheng/p/18353249

相关文章

  • Python和多线程(multi-threading)
    在Python中,实现并行处理的方法有几种,但由于Python的全局解释器锁(GIL,GlobalInterpreterLock)的存在,传统意义上的多线程(使用threading模块)并不总能有效利用多核CPU来实现真正的并行计算。GIL确保任何时候只有一个线程在执行Python字节码。不过,仍然有几种方法可以绕过这个限制,......
  • Python基础知识:奠定坚实的编程基础
    Python,作为一种备受欢迎的高级编程语言,凭借其简洁的语法、强大的标准库和丰富的第三方模块,赢得了广泛的赞誉和应用。无论你是编程新手还是资深开发者,Python都能提供丰富的学习机会和实际应用场景。本文旨在深入探讨Python的基础知识,涵盖字面量、变量、数据类型、注释、类型......
  • python图片压缩大小及设置图片像素大小120乘160
    python图片压缩大小及设置图片像素大小120乘1601.在pycharm中安装pillow库的步骤:打开设置并转到“项目”页面。选择“python解释器”并单击“+”按钮。搜索“pillow”,选中包并单击“安装包”。1.打开PyCharm设置Windows:File>Settings2.转到“项目”页面在左侧导航栏中,单......
  • Python之赋值语句(多重赋值和交换赋值)
    这是《Python入门经典以解决计算问题为导向的Python编程实践》73-74页关于赋值的内容。讲了Python中几种赋值方式。赋值语句1、最简单的赋值:a=b2、多重赋值:a,b,c=1,2,33、交换:a,b=b,a1、最简单的赋值:a=bb可以是数字、字符串,也可以是一个表达式。a必须是一个对象,不......
  • Python使用PyCharm创建一个简单的Qt Quick应用程序-hello_world_quick.py(读取qml文件
    """CreateaSimpleQuickApplication"""importsysfrompathlibimportPathfromPySide6.QtGuiimportQGuiApplicationfromPySide6.QtQmlimportQQmlApplicationEngine#打开文件,读取文件,并返回文件内容defread_file(file_path):"......
  • Python使用PyCharm创建一个简单的Qt Widgets应用程序-hello_world.py
    """CreateaSimpleQtWidgetsApplication"""importrandomimportsysfromPySide6importQtCore,QtWidgets#MainClassclassMyWidget(QtWidgets.QWidget):def__init__(self):super().__init__()self......
  • Python使用PyCharm创建一个简单的Qt Quick应用程序-hello_world_quick.py
    """CreateaSimpleQuickApplication"""importsysfromPySide6.QtGuiimportQGuiApplicationfromPySide6.QtQmlimportQQmlApplicationEngineQML="""importQtQuickimportQtQuick.ControlsimportQtQuick.Lay......
  • Python数据结构:列表详解(创建、访问、修改、列表方法)①
    @[toc]Python中的列表是一个非常强大的数据结构,它允许我们存储、访问和操作一系列的数据。列表可以包含任何类型的对象,包括数字、字符串、甚至其他列表。本文将详细介绍Python列表的创建、访问、修改以及列表方法,并附上一个综合的例子,全面展示列表在实际编程中的应用。一......
  • python由tif影像绘制png图片并制作动图
    笔者最近需要下载一个区域的长时间序列影像,然后将其制作成动图展示其动态变化过程。这其中涉及到两个问题,一是将tif数据绘制成PNG或jpg等格式图片,二是由图片绘制动图GIF。一、由TIF绘制PNG 这里需要用到matplotlib和GDAL两个库来将tif格式图像绘制为PNG图片,如果没有相关包......
  • 多种优化算法优化LSTM——【包括:粒子群、蝙蝠、正余旋、多元宇宙、正余弦、JAYA、哈里
     ......