pypy异步gRPC实现

官网介绍

https://pypi.org/project/grpclib/#example

 

准备环境

复制代码
# python3安装
pip install grpcio==1.41.0
pip install grpcio-tools==1.41.0
pip install grpclib # pypy3.8安装 pip install grpclib pip install protobuf 注意:grpcio和grpcio
-tools包在运行时是不需要的,grpcio-tools包只在代码生成时使用。
复制代码

 

示例

最终目录结构

复制代码
.
├── client.py
├── proto
│   ├── helloworld_grpc.py
│   ├── helloworld_pb2.py
│   ├── helloworld.proto
│   └── __pycache__
│       ├── helloworld_grpc.pypy38.pyc
│       └── helloworld_pb2.pypy38.pyc
└── server.py
复制代码

 

定义proto协议文件

 proto/helloworld.proto

复制代码
syntax = "proto3";

option go_package = "./;proto";

package helloWorld;

message HelloRequest {
    string name = 1;
}

message HelloReply {
    string message = 1;
}

service Greeter {
    rpc SayHello (HelloRequest) returns (HelloReply) {}
}
复制代码

 

python3 编译生成代码

python3  -m grpc_tools.protoc -I. --python_out=. --grpclib_python_out=.  helloworld.proto

 

server代码

复制代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @time: 2021/12/26 4:32 下午
import asyncio
from grpclib.utils import graceful_exit
from grpclib.server import Server, Stream

# generated by protoc
from proto.helloworld_pb2 import HelloReply
from proto.helloworld_grpc import GreeterBase


class Greeter(GreeterBase):

    async def SayHello(self, stream: Stream):
        request = await stream.recv_message()
        print('SayHello start')
        message = f'Hello, {request.name}!'
        await asyncio.sleep(1)
        print(message)
        await stream.send_message(HelloReply(message=message))


async def main(*, host='127.0.0.1', port=50051):
    server = Server([Greeter()])
    # Note: graceful_exit isn't supported in Windows
    with graceful_exit([server]):
        await server.start(host, port)
        print(f'Serving on {host}:{port}')
        await server.wait_closed()
        print('Server closed')


if __name__ == '__main__':
    asyncio.run(main())
复制代码

 

client代码

复制代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @time: 2021/12/26 4:32 下午
import asyncio

from grpclib.client import Channel

# generated by protoc
from proto.helloworld_pb2 import HelloRequest
from proto.helloworld_grpc import GreeterStub


async def main():
    async with Channel('127.0.0.1', 50051) as channel:
        greeter = GreeterStub(channel)

        # reply = await greeter.SayHello(HelloRequest(name='Dr. Strange 1'))
        # print(reply.message)
        task_list = []

        for i in range(10):
            task = asyncio.create_task(greeter.SayHello(HelloRequest(name=f'Dr. Strange {i}')))
            task_list.append(task)

        await asyncio.wait(task_list)


if __name__ == '__main__':
    asyncio.run(main())
复制代码

 

最终运行结果

复制代码
[locusts_test@jenkins gprc异步]$ pypy3.8 server.py 
Serving on 127.0.0.1:50051
SayHello start
SayHello start
SayHello start
SayHello start
SayHello start
SayHello start
SayHello start
SayHello start
SayHello start
SayHello start
Hello, Dr. Strange 0!
Hello, Dr. Strange 1!
Hello, Dr. Strange 2!
Hello, Dr. Strange 3!
Hello, Dr. Strange 4!
Hello, Dr. Strange 5!
Hello, Dr. Strange 6!
Hello, Dr. Strange 7!
Hello, Dr. Strange 8!
Hello, Dr. Strange 9!

[locusts_test@jenkins gprc异步]$ pypy3.8 client.py 
复制代码

 

ps模块导入问题

修改 proto/helloworld_grpc.py 文件中 import helloworld_pb2 为相对导入 from . import helloworld_pb2

 

posted @   LiShiChao  阅读(269)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示