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 ...
随机推荐
- 人工智能论文解读精选 | PRGC:一种新的联合关系抽取模型
NLP论文解读 原创•作者 | 小欣 论文标题:PRGC: Potential Relation and Global Correspondence Based Joint Relational ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
- 【LeetCode】300. Longest Increasing Subsequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 1298 - One Theorem, One Year
1298 - One Theorem, One Year PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...
- bootstrap可编辑下拉框jquery.editable-select
搜了半天发现在某处下载jquery.editable-select需要积分,于是整理出来方便 其他人. 先上下载链接: http://pan.baidu.com/s/1kUXvwlL pas ...
- 一个简单的js时钟
演示地址 代码 <html> <head> <title> Nonove js clock 时钟 </title> <script type=&q ...
- Chapter 7 Confounding
目录 7.1 The structure of confounding Confounding and exchangeability Confounding and the backdoor cri ...
- FreeBSD 物理机下显卡的配置
FreeBSD 已从 Linux 移植了显卡驱动,理论上,A 卡 N 卡均可在 amd64 架构上正常运行. 支持情况 对于 FreeBSD 11,支持情况同 Linux 内核 4.11: 对于 Fr ...
- 基于Spring MVC + Spring + MyBatis的【超市会员管理系统】
资源下载: https://download.csdn.net/download/weixin_44893902/22035329 一. 语言和环境 实现语言:JAVA语言. 使用:MyEclipse ...
- mysql 语句中 sum函数求和 null 变 0
https://blog.csdn.net/Z_passionate/article/details/83821039