pypy异步gRPC实现

官网介绍

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

 

准备环境

# python3安装
pip install grpclib
pip install grpcio-tools

# 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

 

上一篇:elk监听Java日志发送微信报警


下一篇:GRPC 的超时机制