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的ORM框架SQLObject入门实例
Apr 28 Python
利用Python如何生成随机密码
Apr 20 Python
Python实现登录接口的示例代码
Jul 21 Python
python实现遍历文件夹修改文件后缀
Aug 28 Python
详解Python3序列赋值、序列解包
May 14 Python
pandas.DataFrame的pivot()和unstack()实现行转列
Jul 06 Python
Django中使用极验Geetest滑动验证码过程解析
Jul 31 Python
使用django和vue进行数据交互的方法步骤
Nov 11 Python
在 Linux/Mac 下为Python函数添加超时时间的方法
Feb 20 Python
Python 自由定制表格的实现示例
Mar 20 Python
Python定义一个Actor任务
Jul 29 Python
详解Python中下划线的5种含义
Jul 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
php中url传递中文字符,特殊危险字符的解决方法
2013/08/17 PHP
PHP中array_map与array_column之间的关系分析
2014/08/19 PHP
php中实现可以返回多个值的函数实例
2015/03/21 PHP
PHP图片裁剪与缩放示例(无损裁剪图片)
2017/02/08 PHP
yii框架redis结合php实现秒杀效果(实例代码)
2017/10/26 PHP
PHP 实现base64编码文件上传出现问题详解
2020/09/01 PHP
js模仿html5 placeholder适应于不支持的浏览器
2013/01/13 Javascript
js判断选择时间不能小于当前时间的示例代码
2013/09/24 Javascript
js中top的作用深入剖析
2014/03/04 Javascript
使用jquery.validate自定义方法实现&quot;手机号码或者固话至少填写一个&quot;的逻辑验证
2014/09/01 Javascript
jquery制作select列表双向选择示例代码
2014/09/02 Javascript
浅谈Jquery核心函数
2015/06/18 Javascript
JS基于myFocus库实现各种功能的tab选项卡切换效果
2015/09/19 Javascript
jQuery自定义动画函数实例详解(附demo源码)
2015/12/10 Javascript
浅析AMD CMD CommonJS规范--javascript模块化加载学习心得总结
2016/03/16 Javascript
js实现纯前端的图片预览
2016/04/27 Javascript
jQuery实现的纵向下拉菜单实例详解【附demo源码下载】
2016/07/09 Javascript
jQuery实现给input绑定回车事件的方法
2017/02/09 Javascript
jQuery插件之validation插件
2017/03/29 jQuery
Angular4实现动态添加删除表单输入框功能
2017/08/11 Javascript
微信小程序使用wx.request请求服务器json数据并渲染到页面操作示例
2019/03/30 Javascript
vue学习笔记之slot插槽用法实例分析
2020/02/29 Javascript
matplotlib.pyplot.matshow 矩阵可视化实例
2020/06/16 Python
通过代码实例了解Python异常本质
2020/09/16 Python
CSS Grid布局教程之网格单元格布局
2014/12/30 HTML / CSS
Gweniss格温妮丝女包官网:英国纯手工制造潮流包包品牌
2018/02/07 全球购物
印度购买眼镜和太阳镜网站:Coolwinks
2018/09/26 全球购物
美国转售二手商品的电子商务平台:BLINQ
2018/12/13 全球购物
Ibatis中如何提高SQL Map的性能
2013/05/11 面试题
会计专业自我鉴定范文
2013/10/06 职场文书
办加油卡单位介绍信
2014/01/09 职场文书
网络信息安全承诺书
2014/03/26 职场文书
2014年基层党组织公开承诺书
2014/03/29 职场文书
在职人员跳槽求职信
2015/03/20 职场文书
vue代码分块和懒加载非必要资源文件
2022/04/11 Vue.js
解决 Redis 秒杀超卖场景的高并发
2022/04/12 Redis