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利用装饰器进行运算的实例分析
Aug 04 Python
python对json的相关操作实例详解
Jan 04 Python
对pandas中时间窗函数rolling的使用详解
Nov 28 Python
浅谈Python中的可迭代对象、迭代器、For循环工作机制、生成器
Mar 11 Python
python2和python3实现在图片上加汉字的方法
Aug 22 Python
Python3实现将一维数组按标准长度分隔为二维数组
Nov 29 Python
flask实现验证码并验证功能
Dec 05 Python
Pandas —— resample()重采样和asfreq()频度转换方式
Feb 26 Python
python实现录屏功能(亲测好用)
Mar 02 Python
python 可视化库PyG2Plot的使用
Jan 21 Python
python 制作网站筛选工具(附源码)
Jan 21 Python
Python对excel的基本操作方法
Feb 18 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显卡排行榜天梯图 显卡天梯图2020年3月最新版
2020/04/02 数码科技
C/S和B/S两种架构区别与优缺点分析
2014/10/23 PHP
php中get_magic_quotes_gpc()函数说明
2017/02/06 PHP
PHP使用OB缓存实现静态化功能示例
2019/03/23 PHP
PHP Beanstalkd消息队列的安装与使用方法实例详解
2020/02/21 PHP
北京奥运官方网站幻灯切换效果flash版打包下载
2008/01/30 Javascript
JS 强制设为首页的代码
2009/01/31 Javascript
JavaScript 事件记录使用说明
2009/10/20 Javascript
select、radio表单回显功能实现避免使用jquery载入赋值
2013/06/08 Javascript
javascript 自定义回调函数示例代码
2014/09/26 Javascript
node.js中的path.sep方法使用说明
2014/12/08 Javascript
jQuery应用之jQuery链用法实例
2015/01/19 Javascript
JavaScript判断数组是否包含指定元素的方法
2015/07/01 Javascript
javascript类型系统——undefined和null全面了解
2016/07/13 Javascript
基于JavaScript实现的插入排序算法分析
2017/04/14 Javascript
HTML5开发Kinect体感游戏的实例应用
2017/09/18 Javascript
vue-cli + sass 的正确打开方式图文详解
2017/10/27 Javascript
vue 下列表侧滑操作实例代码详解
2018/07/24 Javascript
详解使用element-ui table组件的筛选功能的一个小坑
2018/11/02 Javascript
Vue 3.0双向绑定原理的实现方法
2019/10/23 Javascript
[33:19]完美世界DOTA2联赛PWL S2 PXG vs InkIce 第一场 11.26
2020/11/30 DOTA
在Django的视图中使用数据库查询的方法
2015/07/16 Python
PyTorch CNN实战之MNIST手写数字识别示例
2018/05/29 Python
Python解决走迷宫问题算法示例
2018/07/27 Python
对python numpy.array插入一行或一列的方法详解
2019/01/29 Python
对django的User模型和四种扩展/重写方法小结
2019/08/17 Python
python with语句的原理与用法详解
2020/03/30 Python
50个强大璀璨的CSS3/JS技术运用实例
2010/02/27 HTML / CSS
印尼值得信赖的在线交易网站:Bukalapak
2019/03/11 全球购物
用JAVA SOCKET编程,读服务器几个字符,再写入本地显示
2012/11/25 面试题
优秀的自荐信要注意哪些
2014/01/03 职场文书
展会邀请函范文
2014/01/26 职场文书
八一慰问活动方案
2014/02/07 职场文书
大型公益活动策划方案
2014/08/20 职场文书
MYSQL如何查看进程和kill进程
2022/03/13 MySQL
Python中time标准库的使用教程
2022/04/13 Python