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 相关文章推荐
基于scrapy的redis安装和配置方法
Jun 13 Python
Python实现图片拼接的代码
Jul 02 Python
python3使用pandas获取股票数据的方法
Dec 22 Python
Python3.5迭代器与生成器用法实例分析
Apr 30 Python
Python爬虫实现验证码登录代码实例
May 10 Python
对pytorch中的梯度更新方法详解
Aug 20 Python
Python 脚本拉取 Docker 镜像问题
Nov 10 Python
python保存log日志,实现用log日志画图
Dec 24 Python
pymysql的简单封装代码实例
Jan 08 Python
完美解决jupyter由于无法import新包的问题
May 26 Python
Python+Selenium随机生成手机验证码并检查页面上是否弹出重复手机号码提示框
Sep 21 Python
用python获取txt文件中关键字的数量
Dec 24 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
PHP系统流量分析的程序
2006/10/09 PHP
PHP的cURL库功能简介 抓取网页、POST数据及其他
2011/04/07 PHP
Laravel中日期时间处理包Carbon的简单使用
2017/09/21 PHP
JS 参数传递的实际应用代码分析
2009/09/13 Javascript
Javascript 日期处理之时区问题
2009/10/08 Javascript
查询绑定数据岛的表格中的文本并修改显示方式的js代码
2009/12/15 Javascript
从jQuery.camelCase()学习string.replace() 函数学习
2011/09/13 Javascript
Javascript中的delete操作符详细介绍
2014/06/06 Javascript
JS动画效果打开、关闭层的实现方法
2015/05/09 Javascript
基于JS实现简单的样式切换效果代码
2015/09/04 Javascript
js电话号码验证方法
2015/09/28 Javascript
jQuery插件echarts设置折线图中折线线条颜色和折线点颜色的方法
2017/03/03 Javascript
angular框架实现全选与单选chekbox的自定义
2017/07/06 Javascript
微信小程序Echarts图表组件使用方法详解
2019/06/25 Javascript
JS实现简易计算器
2020/02/14 Javascript
vue离开当前页面触发的函数代码
2020/09/01 Javascript
[00:09]DOTA2全国高校联赛 精彩活动引爆全场
2018/05/30 DOTA
[01:32:10]NAVI vs VG Supermajor 败者组 BO3 第一场 6.5
2018/06/06 DOTA
Python help()函数用法详解
2014/03/11 Python
python 中的divmod数字处理函数浅析
2017/10/17 Python
python实现点击按钮修改数据的方法
2019/07/17 Python
python飞机大战 pygame游戏创建快速入门详解
2019/12/17 Python
python实现处理mysql结果输出方式
2020/04/09 Python
The Athlete’s Foot新西兰:新西兰最大的运动鞋零售商
2019/12/23 全球购物
大学生个人实习的自我评价
2014/02/15 职场文书
公安机关纪律作风整顿剖析
2014/10/10 职场文书
贵阳市党的群众路线教育实践活动党(工)委领导班子整改方案
2014/10/26 职场文书
安全责任书
2015/01/29 职场文书
搞笑婚前保证书
2015/02/28 职场文书
学校重阳节活动总结
2015/03/24 职场文书
2015年科室工作总结
2015/04/10 职场文书
社区节水倡议书
2015/04/29 职场文书
2019大学生社会实践报告汇总
2019/08/16 职场文书
python数据可视化使用pyfinance分析证券收益示例详解
2021/11/20 Python
解决IDEA翻译插件Translation报错更新TTK失败不能使用
2022/04/24 Python
MySQL transaction事务安全示例讲解
2022/06/21 MySQL