python中grpc配置asyncio使用
python中grpc配置asyncio使用
安装grpclib
pip3 install grpclibprotoc编译.proto文件,生成源码文件
python -m grpc_tools.protoc -I. --python_out=. --grpclib_python_out=. helloworld.proto
grpclib github官网helloworld.proto文件代码
syntax = "proto3";
option go_package = ".;proto";
service Greeter {
rpc SayHello(HelloRequest) returns (HelloReply);
}
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}
- server.py
import asyncio
# from grpclib.utils import graceful_exit
from grpclib.server import Server
# generated by protoc
from grpclib_test.helloworld_pb2 import HelloReply
from grpclib_test.helloworld_grpc import GreeterBase
class Greeter(GreeterBase):
async def SayHello(self, stream):
request = await stream.recv_message()
message = f'Hello, {request.name}!'
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()
if __name__ == '__main__':
asyncio.run(main())
- client.py
import asyncio
from grpclib.client import Channel
# generated by protoc
from grpclib_test.helloworld_pb2 import HelloRequest, HelloReply
from grpclib_test.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="马亚南"))
print(reply.message)
if __name__ == '__main__':
asyncio.run(main())
总结:客户端可以使用asyncio发起连接,服务端使用了asyncio可以维护大量的连接。
疑问:这个会影响go和python之间的相互调用吗?
我们用go client请求一下试试:
package main
import (
"context"
"goRPC/grpc_test/proto"
"google.golang.org/grpc"
)
func main() {
conn, _ := grpc.Dial("127.0.0.1:50051", grpc.WithInsecure())
defer conn.Close()
client := proto.NewGreeterClient(conn)
rsp, _ := client.SayHello(context.Background(), &proto.HelloRequest{Name: "马亚南"})
print(rsp.Message)
}
请求一切正常。
python中grpc配置asyncio使用的更多相关文章
- python中利用队列asyncio.Queue进行通讯详解
python中利用队列asyncio.Queue进行通讯详解 本文主要给大家介绍了关于python用队列asyncio.Queue通讯的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细 ...
- YAML 在Python中的配置应用
环境搭建 YAML语法 语法规则 数据结构 列表数组 原子量 YAML应用 案例 load dump 总结 YAML是一个堪比XML,JSON数据格式的更加方便,简洁的,易于人眼阅读的序列化数据格式. ...
- python 中pip配置清华源
anaconda配置镜像 Mac and Linux conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda ...
- 在python中Flask配置服务
from flask import Flask, request from data_util import UtilsLTPTranslate import json model = UtilsLT ...
- Python虚拟环境的配置
Python中,配置虚拟环境主要是为了防止版本之间的冲突,我是这么理解的: 1.用虚拟环境可以在一个电脑中使用多个Python解释器以及扩展: 2.可以方便的在同一台电脑中使用多个版本的代码. 虚拟环 ...
- Python flask中的配置
当你开始学习Flask时,配置看上去是小菜一碟.你仅仅需要在config.py定义几个变量,然后万事大吉. 然而当你不得不管理一个生产上的应用的配置时,这一切将变得棘手万分. 你不得不设法保护API密 ...
- gRPC 在 Python中的应用
python -m grpc_tools.protoc --proto_path=. --python_out=. --grpc_python_out=. hello.proto 简介 在python ...
- (转)python中调用R语言通过rpy2 进行交互安装配置详解
python中调用R语言通过rpy2 进行交互安装配置详解(R_USER.R_HOME配置) 2018年11月08日 10:00:11 luqin_ 阅读数:753 python中调用R语言通过r ...
- How to Configure Eclipse for Python --- 在eclipse中如何配置pydev
From: http://www.rose-hulman.edu/class/csse/resources/Eclipse/eclipse-python-configuration.htm Pytho ...
随机推荐
- React-Router(二)
URL参数部分 import React from "react"; import { BrowserRouter as Router, Switch, Route, Link, ...
- centos下修改hosts文件以及生效命令
修改 vim /etc/hosts 生效 service network restart 或者 /etc/init.d/network restart
- 输入npm install 报错npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! node-sass@4.13.1 postinstall: `node scripts/build.js`
输入npm install 报以下错误 npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! node-sass@4.13.1 postinstall: ...
- 【LeetCode】264. Ugly Number II 解题报告(Java & Python)
标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ https://leetcode.com/prob ...
- 【LeetCode】1154. Day of the Year 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 计算1月1号之间天数 日期 题目地址:https:// ...
- Mysterious For(hdu4373)
Mysterious For Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- 前端项目 node8升级到node16,代码升级汇总
背景 公司的项目是vue项目,环境是node@8x版本的,最近我创建react hook的项目,发现至少需要node14才支持,打开官网才发现node都已经到16版本了.失策啊,失策.于是直接升级到最 ...
- Spring MVC 文件上传、Restful、表单校验框架
目录 文件上传 Restful Restful 简介 Rest 行为常用约定方式 Restful开发入门 表单校验框架 表单校验框架介绍 快速入门 多规则校验 嵌套校验 分组校验 综合案例 实用校验范 ...
- Augmentation For GAN
目录 概 主要内容 Differentiable Augmentation Adaptive Augmentation 代码 Zhao S., Liu Z., Lin J., Zhu J. and H ...
- Java得到当前时间并格式化输出
代码: SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间 sdf.applyPattern("yyyy-MM-dd HH:mm:s ...