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爬取斗鱼app中照片方法实例
Dec 03 Python
python调用摄像头显示图像的实例
Aug 03 Python
Python中new方法的详解
Jan 15 Python
Python2和Python3的共存和切换使用
Apr 12 Python
Django MEDIA的配置及用法详解
Jul 25 Python
Python实现的微信红包提醒功能示例
Aug 22 Python
在vscode中配置python环境过程解析
Sep 28 Python
如何使用python代码操作git代码
Feb 29 Python
python matplotlib imshow热图坐标替换/映射实例
Mar 14 Python
Pycharm中使用git进行合作开发的教程详解
Nov 17 Python
python re模块常见用法例举
Mar 01 Python
pygame面向对象的飞行小鸟实现(Flappy bird)
Apr 01 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简单socket服务器客户端代码实例
2015/05/18 PHP
Yii2中datetime类的使用
2016/12/17 PHP
MSN消息提示类
2006/09/05 Javascript
jQuery实现数字加减效果汇总
2014/12/16 Javascript
jQuery实现提交按钮点击后变成正在处理字样并禁止点击的方法
2015/03/24 Javascript
JavaScript的RequireJS库入门指南
2015/07/01 Javascript
JavaScript实现为input与textarea自定义hover,focus效果的方法
2015/08/21 Javascript
WordPress 单页面上一页下一页的实现方法【附代码】
2016/03/10 Javascript
Bootstrap安装环境配置教程分享
2016/05/27 Javascript
BootStrap实现邮件列表的分页和模态框添加邮件的功能
2016/10/13 Javascript
JS函数多个参数默认值指定方法分析
2016/11/28 Javascript
nodejs中sleep功能实现暂停几秒的方法
2017/07/12 NodeJs
Vue-Cli中自定义过滤器的实现代码
2017/08/12 Javascript
vue中$refs的用法及作用详解
2018/04/24 Javascript
JavaScript的词法结构精华篇
2018/10/17 Javascript
vue实现鼠标移入移出事件代码实例
2019/03/27 Javascript
详解vue项目打包步骤
2019/03/29 Javascript
JavaScript原型式继承实现方法
2019/11/06 Javascript
微信小程序wx.navigateTo方法里的events参数使用详情及场景
2020/01/07 Javascript
Openlayers学习之地图比例尺控件
2020/09/28 Javascript
[01:15:18]2014 DOTA2国际邀请赛中国区预选赛 LGD VS Speed Gaming.cn
2014/05/22 DOTA
Django项目中用JS实现加载子页面并传值的方法
2018/05/28 Python
在PyCharm下使用 ipython 交互式编程的方法
2019/01/17 Python
使用Python的SymPy库解决数学运算问题的方法
2019/03/27 Python
python如何求数组连续最大和的示例代码
2020/02/04 Python
超级实用的8个Python列表技巧
2020/08/24 Python
2014年创先争优活动总结
2014/05/04 职场文书
绿色环保标语
2014/06/12 职场文书
安全保卫工作竞聘材料
2014/08/25 职场文书
2014年效能监察工作总结
2014/11/21 职场文书
小学教师2014年度工作总结
2014/12/03 职场文书
销售2014年度工作总结
2014/12/08 职场文书
好人好事新闻稿
2015/07/17 职场文书
安全教育培训心得体会
2016/01/15 职场文书
2022新作动画《福星小子》释出宣传影片 加入内田真礼&宫野真守配音演出
2022/04/08 日漫
vue router 动态路由清除方式
2022/05/25 Vue.js