Python grpc超时机制代码示例


Posted in Python onSeptember 14, 2020

工作中遇到一个问题,上游服务通过grpc调用下游服务,但是由于下游服务负载太高导致上游服务的调用会随机出现超时的情况,但是有一点不太明确:超时之后,下游服务还会继续进行计算么?

于是自己写了一个damon试了一下:

client:

# Copyright 2015 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""The Python implementation of the GRPC helloworld.Greeter client."""

from __future__ import print_function
import logging

import grpc

import helloworld_pb2
import helloworld_pb2_grpc


def run():
  # NOTE(gRPC Python Team): .close() is possible on a channel and should be
  # used in circumstances in which the with statement does not fit the needs
  # of the code.
  with grpc.insecure_channel('localhost:50051') as channel:
    stub = helloworld_pb2_grpc.GreeterStub(channel)
    response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'), timeout=30)
  print("Greeter client received: " + response.message)


if __name__ == '__main__':
  logging.basicConfig()
  run()

server:

# Copyright 2015 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""The Python implementation of the GRPC helloworld.Greeter server."""

from concurrent import futures
import time
import logging

import grpc

import helloworld_pb2
import helloworld_pb2_grpc

_ONE_DAY_IN_SECONDS = 60 * 60 * 24


class Greeter(helloworld_pb2_grpc.GreeterServicer):

  def SayHello(self, request, context):
  count = 0
  while count < 10:
    print('time:%s' % (time.time()))
    time.sleep(5)
    return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)


def serve():
  server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
  helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
  server.add_insecure_port('[::]:50051')
  server.start()
  try:
    while True:
      time.sleep(_ONE_DAY_IN_SECONDS)
  except KeyboardInterrupt:
    server.stop(0)


if __name__ == '__main__':
  logging.basicConfig()
  serve()

这两个例子就是在grpc官方提供的python例子上做了一下小的改动,得到的结果是:当client超时报错退出之后,server还是会继续进行计算,直到结束,那如果是这样的话,超时的机制对于server来说是没有作用的,即使client已经不再等待这个结果了,但是server还是会继续计算,浪费server的资源。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python采用Django制作简易的知乎日报API
Aug 03 Python
浅谈Python由__dict__和dir()引发的一些思考
Oct 30 Python
Python实现批量读取图片并存入mongodb数据库的方法示例
Apr 02 Python
pandas.loc 选取指定列进行操作的实例
May 18 Python
Sanic框架应用部署方法详解
Jul 18 Python
python实现停车管理系统
Nov 30 Python
用python 实现在不确定行数情况下多行输入方法
Jan 28 Python
python实现简单聊天室功能 可以私聊
Jul 12 Python
为什么从Python 3.6开始字典有序并效率更高
Jul 15 Python
PyTorch里面的torch.nn.Parameter()详解
Jan 03 Python
学习Python需要哪些工具
Sep 04 Python
Python3中小括号()、中括号[]、花括号{}的区别详解
Nov 15 Python
python/golang 删除链表中的元素
Sep 14 #Python
Python基于pillow库实现生成图片水印
Sep 14 #Python
python/golang实现循环链表的示例代码
Sep 14 #Python
python实现canny边缘检测
Sep 14 #Python
Python gevent协程切换实现详解
Sep 14 #Python
通过实例了解python__slots__使用方法
Sep 14 #Python
python如何遍历指定路径下所有文件(按按照时间区间检索)
Sep 14 #Python
You might like
海贼王:最美的悬赏令!
2020/03/02 日漫
thinkphp中字符截取函数msubstr()用法分析
2016/01/09 PHP
php 中的closure用法详解
2017/06/12 PHP
javascript引用对象的方法代码
2007/08/13 Javascript
JS保留两位小数 四舍五入函数的小例子
2013/11/20 Javascript
ie浏览器使用js导出网页到excel并打印
2014/03/11 Javascript
Laydate时间组件在火狐浏览器下有多时间输入框时只能给第一个输入框赋值的解决方法
2016/08/18 Javascript
JavaScript登录验证基础教程
2017/11/01 Javascript
jQuery实现带右侧索引功能的通讯录示例【附源码下载】
2018/04/17 jQuery
对vue中methods互相调用的方法详解
2018/08/30 Javascript
Angular父子组件通过服务传参的示例方法
2018/10/31 Javascript
微信小程序学习笔记之本地数据缓存功能详解
2019/03/29 Javascript
Vue+tracking.js 实现前端人脸检测功能
2020/04/16 Javascript
ElementUI 修改默认样式的几种办法(小结)
2020/07/29 Javascript
Swiper实现导航栏滚动效果
2020/10/16 Javascript
Python针对给定列表中元素进行翻转操作的方法分析
2018/04/27 Python
Python实现朴素贝叶斯分类器的方法详解
2018/07/04 Python
python实现多层感知器
2019/01/18 Python
PyQt5 实现字体大小自适应分辨率的方法
2019/06/18 Python
python Gunicorn服务器使用方法详解
2019/07/22 Python
Pytorch GPU显存充足却显示out of memory的解决方式
2020/01/13 Python
检测tensorflow是否使用gpu进行计算的方式
2020/02/03 Python
使用Python三角函数公式计算三角形的夹角案例
2020/04/15 Python
Python性能分析工具py-spy原理用法解析
2020/07/27 Python
使用HTML5加载音频和视频的实现代码
2020/11/30 HTML / CSS
StubHub德国:购买和出售门票
2017/09/06 全球购物
华硕新加坡官方网上商店:ASUS Singapore
2020/07/09 全球购物
实习老师离校感言
2014/02/03 职场文书
高中生第一学年自我鉴定
2014/09/12 职场文书
村党支部对照检查材料思想汇报
2014/09/28 职场文书
2014年酒店前台工作总结
2014/11/14 职场文书
2014年电话客服工作总结
2014/12/09 职场文书
2015年社区反邪教工作总结
2015/10/14 职场文书
2016年社区“6.26”禁毒日宣传活动总结
2016/04/05 职场文书
2016年推广普通话宣传周活动总结
2016/04/06 职场文书
mysql中整数数据类型tinyint详解
2021/12/06 MySQL