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 正则表达式 概述及常用字符
May 04 Python
使用BeautifulSoup爬虫程序获取百度搜索结果的标题和url示例
Jan 19 Python
win7上python2.7连接mysql数据库的方法
Jan 14 Python
python 调用win32pai 操作cmd的方法
May 28 Python
基于Python实现的ID3决策树功能示例
Jan 02 Python
python list是否包含另一个list所有元素的实例
May 04 Python
python 3.7.0 安装配置方法图文教程
Aug 27 Python
pycharm在调试python时执行其他语句的方法
Nov 29 Python
Django CBV与FBV原理及实例详解
Aug 12 Python
Python 读取用户指令和格式化打印实现解析
Sep 02 Python
Python基础之字符串操作常用函数集合
Feb 09 Python
Python利用Turtle绘制哆啦A梦和小猪佩奇
Apr 04 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 日期加减的类,很不错
2009/10/10 PHP
关于访问控制的一首PHP面试题(对属性或方法的访问控制)
2012/09/13 PHP
Laravel 微信小程序后端实现用户登录的示例代码
2019/11/26 PHP
tp5.1 框架查询表达式用法详解
2020/05/25 PHP
jquery+json 通用三级联动下拉列表
2010/04/19 Javascript
jquery 选取方法都有哪些
2014/05/18 Javascript
TinyMCE提交AjaxForm获取不到数据的解决方法
2015/03/05 Javascript
酷炫jQuery全屏3D焦点图动画效果
2016/03/22 Javascript
深入理解setTimeout函数和setInterval函数
2016/05/20 Javascript
jQuery通过ajax快速批量提交表单数据
2016/10/25 Javascript
jquery hover 不停闪动问题的解决方法(亦为stop()的使用)
2017/02/10 Javascript
canvas实现刮刮卡效果
2017/03/14 Javascript
vue-cli脚手架引入图片的几种方法总结
2018/03/13 Javascript
Layui之table中的radio在切换分页时无法记住选中状态的解决方法
2019/09/02 Javascript
解决webpack多页面内存溢出的方法示例
2019/10/08 Javascript
js实现自动播放匀速轮播图
2020/02/06 Javascript
Vue开发中遇到的跨域问题及解决方法
2020/02/11 Javascript
JsonServer安装及启动过程图解
2020/02/28 Javascript
用JS实现选项卡
2020/03/23 Javascript
vue使用swiper实现左右滑动切换图片
2020/10/16 Javascript
vue实现顶部菜单栏
2020/11/08 Javascript
python中self原理实例分析
2015/04/30 Python
Python Requests 基础入门
2016/04/07 Python
python魔法方法-自定义序列详解
2016/07/21 Python
Linux下多个Python版本安装教程
2018/08/15 Python
Python(PyS60)实现简单语音整点报时
2019/11/18 Python
Pytorch 保存模型生成图片方式
2020/01/10 Python
python 实现多维数组(array)排序
2020/02/28 Python
django在开发中取消外键约束的实现
2020/05/20 Python
基于Pyinstaller打包Python程序并压缩文件大小
2020/05/28 Python
环境工程大学生个人的自我评价
2013/10/08 职场文书
财务部总监岗位职责
2014/03/12 职场文书
工程学毕业生自荐信
2014/06/14 职场文书
项目负责人岗位职责
2015/02/15 职场文书
护理培训心得体会
2016/01/22 职场文书
优秀共产党员事迹材料2016
2016/02/29 职场文书