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运行的17个时新手常见错误小结
Aug 07 Python
分享python数据统计的一些小技巧
Jul 21 Python
对python 矩阵转置transpose的实例讲解
Apr 17 Python
详解Django中CBV(Class Base Views)模型源码分析
Feb 25 Python
Python递归函数实例讲解
Feb 27 Python
python操作excel让工作自动化
Aug 09 Python
使用Matplotlib绘制不同颜色的带箭头的线实例
Apr 17 Python
浅谈keras保存模型中的save()和save_weights()区别
May 21 Python
关于Python3爬虫利器Appium的安装步骤
Jul 29 Python
Python之字典对象的几种创建方法
Sep 30 Python
python“静态”变量、实例变量与本地变量的声明示例
Nov 13 Python
Pandas的数据过滤实现
Jan 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
基于session_unset与session_destroy的区别详解
2013/06/03 PHP
ECMall支持SSL连接邮件服务器的配置方法详解
2014/05/19 PHP
PHP实现获取域名的方法小结
2014/11/05 PHP
php实现字符串首字母转换成大写的方法
2015/03/17 PHP
php 人员权限管理(RBAC)实例(推荐)
2017/05/24 PHP
PHP addAttribute()函数讲解
2019/02/03 PHP
php5与php7的区别点总结
2019/10/11 PHP
jQuery实现的类flash菜单效果代码
2010/05/17 Javascript
Javascript 八进制转义字符(8进制)
2011/04/08 Javascript
JS打开图片另存为对话框实现代码
2012/12/26 Javascript
JavaScript实现拖拽网页内元素的方法
2015/04/15 Javascript
js实现兼容性好的微软官网导航下拉菜单效果
2015/09/07 Javascript
js获取本机操作系统类型的两种方法
2015/12/19 Javascript
移动端H5开发 Turn.js实现很棒的翻书效果
2016/06/20 Javascript
Vue.js手风琴菜单组件开发实例
2017/05/16 Javascript
JavaScript对象_动力节点Java学院整理
2017/06/23 Javascript
关于TypeScript中import JSON的正确姿势详解
2017/07/25 Javascript
Angular中的$watch方法详解
2017/09/18 Javascript
vue中mint-ui的使用方法
2018/04/04 Javascript
js canvas实现红包照片效果
2018/08/21 Javascript
如何通过setTimeout理解JS运行机制详解
2019/03/23 Javascript
JS算法教程之字符串去重与字符串反转
2020/12/15 Javascript
Vue中使用wangeditor富文本编辑的问题
2021/02/07 Vue.js
[36:52]DOTA2真视界:基辅特锦赛总决赛
2017/05/21 DOTA
基于wxpython实现的windows GUI程序实例
2015/05/30 Python
python爬虫获取淘宝天猫商品详细参数
2020/06/23 Python
基于Python log 的正确打开方式
2018/04/28 Python
OpenCV+face++实现实时人脸识别解锁功能
2019/08/28 Python
Python调用Windows命令打印文件
2020/02/07 Python
html5 postMessage解决跨域、跨窗口消息传递方案
2016/12/20 HTML / CSS
“型”走纽约上东区:Sam Edelman
2017/04/02 全球购物
2014年幼儿园元旦活动方案
2014/02/13 职场文书
2014年团工作总结
2014/11/27 职场文书
简历自我评价优缺点
2015/03/11 职场文书
你为什么是穷人?可能是这5个缺点造成
2019/07/11 职场文书
Centos7中MySQL数据库使用mysqldump进行每日自动备份的编写
2021/08/02 MySQL