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之不要红头文件(1)
Sep 28 Python
Python序列操作之进阶篇
Dec 08 Python
使用python实现接口的方法
Jul 07 Python
python实现在IDLE中输入多行的方法
Apr 19 Python
Python3中内置类型bytes和str用法及byte和string之间各种编码转换 问题
Sep 27 Python
python绘制直方图和密度图的实例
Jul 08 Python
Python中Subprocess的不同函数解析
Dec 10 Python
基于python3的socket聊天编程
Feb 17 Python
利用Python制作动态排名图的实现代码
Apr 09 Python
python新手学习使用库
Jun 11 Python
Spy++的使用方法及下载教程
Jan 29 Python
python基于OpenCV模板匹配识别图片中的数字
Mar 31 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
网站当前的在线人数
2006/10/09 PHP
php strrpos()与strripos()函数
2013/08/31 PHP
9条PHP编程小知识及易犯的小错误
2015/01/22 PHP
示例详解Laravel重置密码代码重构
2016/08/10 PHP
PHP正则表达式笔记与实例详解
2019/05/09 PHP
jquery maxlength使用说明
2011/09/09 Javascript
JQuery操作三大控件(下拉,单选,复选)的方法
2013/08/06 Javascript
js函数模拟显示桌面.scf程序示例
2014/04/20 Javascript
使用PHP+JavaScript将HTML页面转换为图片的实例分享
2016/04/18 Javascript
jQuery实现磁力图片跟随效果完整示例
2016/09/16 Javascript
JQuery学习总结【二】
2016/12/01 Javascript
JavaScript的for循环中嵌套一个点击事件的问题解决
2017/03/03 Javascript
jQuery 利用ztree实现树形表格的实例代码
2017/09/27 jQuery
jQuery中复合选择器简单用法示例
2018/03/31 jQuery
js实现带箭头的进度流程
2020/03/26 Javascript
Vue 请求传公共参数的操作
2020/07/31 Javascript
[00:17]游戏风云独家报道:DD赛后说出数字秘密 吓死你们啊!
2014/07/13 DOTA
[01:28]一分钟告诉你DOTA2 TI9不朽宝藏Ⅱ中有什么!
2019/07/09 DOTA
用Python编写分析Python程序性能的工具的教程
2015/04/01 Python
Mac中Python 3环境下安装scrapy的方法教程
2017/10/26 Python
python 求1-100之间的奇数或者偶数之和的实例
2019/06/11 Python
如何通过命令行进入python
2020/07/06 Python
介绍一下Ruby中的对象,属性和方法
2012/07/11 面试题
工程造价管理专业大专生求职信
2013/10/06 职场文书
教师实习自我鉴定
2013/12/18 职场文书
群众路线剖析材料
2014/02/02 职场文书
资源工程专业毕业生求职信
2014/02/27 职场文书
通信工程求职信
2014/07/16 职场文书
2014教师党员自我评议总结
2014/09/19 职场文书
民主生活会发言材料
2014/10/20 职场文书
2014年教育培训工作总结
2014/12/08 职场文书
美丽人生观后感
2015/06/03 职场文书
升学宴学生致辞
2015/07/27 职场文书
2016年推广普通话宣传周活动总结
2016/04/06 职场文书
Python opencv缺陷检测的实现及问题解决
2021/04/24 Python
类和原型的设计模式之复制与委托差异
2022/07/07 Javascript