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实例一个类背后发生了什么
Feb 09 Python
python安装PIL模块时Unable to find vcvarsall.bat错误的解决方法
Sep 19 Python
一个基于flask的web应用诞生 记录用户账户登录状态(6)
Apr 11 Python
Python cookbook(数据结构与算法)保存最后N个元素的方法
Feb 13 Python
Django中STATIC_ROOT和STATIC_URL及STATICFILES_DIRS浅析
May 08 Python
Python 中导入csv数据的三种方法
Nov 01 Python
使用Python快速制作可视化报表的方法
Feb 03 Python
python 设置输出图像的像素大小方法
Jul 04 Python
Python shelve模块实现解析
Aug 28 Python
python 实现简单的计算器(gui界面)
Nov 11 Python
matplotlib对象拾取事件处理的实现
Jan 14 Python
python快速安装OpenCV的步骤记录
Feb 22 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
彻底删除thinkphp3.1案例blog标签的方法
2014/12/05 PHP
laravel 操作数据库常用函数的返回值方法
2019/10/11 PHP
Laravel配合jwt使用的方法实例
2020/10/25 PHP
utf-8编码引起js输出中文乱码的解决办法
2010/06/23 Javascript
Ubuntu 11.10 安装Node.js的方法
2011/11/30 Javascript
JS的document.all函数使用示例
2013/12/30 Javascript
JQuery与Ajax调用新浪API获取短网址的代码
2014/02/07 Javascript
7个JS基础知识总结
2014/03/05 Javascript
一张表格告诉你windows.onload()与$(document).ready()的区别
2014/05/16 Javascript
javascript实现的平方米、亩、公顷单位换算小程序
2014/08/11 Javascript
Jquery实现简单的轮播效果(代码管用)
2016/03/14 Javascript
jquery中实现时间戳与日期相互转换
2016/04/12 Javascript
vue.js全局API之nextTick全面解析
2017/07/07 Javascript
vue跨域解决方法
2017/10/15 Javascript
实例分析JS与Node.js中的事件循环
2017/12/12 Javascript
详解mpvue中使用vant时需要注意的onChange事件的坑
2019/05/16 Javascript
JavaScript遍历数组和对象的元素简单操作示例
2019/07/09 Javascript
解决ant-design-vue中menu菜单无法默认展开的问题
2020/10/31 Javascript
[36:33]Ti4 循环赛第四日 附加赛NEWBEE vs Mouz
2014/07/13 DOTA
[02:41]DOTA2亚洲邀请赛小组赛第三日 赛事回顾
2015/02/01 DOTA
Python切换pip安装源的方法详解
2016/11/18 Python
Python实现生成随机数据插入mysql数据库的方法
2017/12/25 Python
1分钟快速生成用于网页内容提取的xslt
2018/02/23 Python
对python产生随机的二维数组实例详解
2018/12/13 Python
Python 过滤错误log并导出的实例
2019/12/26 Python
Python实现计算长方形面积(带参数函数demo)
2020/01/18 Python
Python reduce函数作用及实例解析
2020/05/08 Python
Python使用文件操作实现一个XX信息管理系统的示例
2020/07/02 Python
解决Python3.8运行tornado项目报NotImplementedError错误
2020/09/02 Python
乡镇干部先进性教育活动个人整改措施
2014/09/16 职场文书
个人四风问题原因分析及整改措施
2014/09/28 职场文书
2015年纪委工作总结
2015/05/13 职场文书
安全教育第一课观后感
2015/06/17 职场文书
2015大学迎新晚会主持词
2015/07/16 职场文书
2016年中秋节晚会领导致辞
2015/11/26 职场文书
班主任班级管理心得体会
2016/01/07 职场文书