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中精确输出JSON浮点数的方法
Apr 18 Python
小米5s微信跳一跳小程序python源码
Jan 08 Python
Python基于生成器迭代实现的八皇后问题示例
May 23 Python
用Cython加速Python到“起飞”(推荐)
Aug 01 Python
django删除表重建的实现方法
Aug 28 Python
Python求正态分布曲线下面积实例
Nov 20 Python
Python list运算操作代码实例解析
Jan 20 Python
解决Tensorboard 不显示计算图graph的问题
Feb 15 Python
Django 项目通过加载不同env文件来区分不同环境
Feb 17 Python
python使用PIL剪切和拼接图片
Mar 23 Python
浅谈Python xlwings 读取Excel文件的正确姿势
Feb 26 Python
关于python爬虫应用urllib库作用分析
Sep 04 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
模拟OICQ的实现思路和核心程序(三)
2006/10/09 PHP
MySQL中create table语句的基本语法是
2007/01/15 PHP
phpmyadmin配置文件现在需要绝密的短密码(blowfish_secret)的2种解决方法
2014/05/07 PHP
php redis实现对200w用户的即时推送
2017/03/04 PHP
tp5框架基于Ajax实现列表无刷新排序功能示例
2020/02/10 PHP
[全兼容哦]--实用、简洁、炫酷的页面转入效果loing
2007/05/07 Javascript
Mootools 1.2教程 同时进行多个形变动画
2009/09/15 Javascript
关于Javascript加载执行优化的研究报告
2014/12/16 Javascript
jquery zTree异步加载简单实例讲解
2016/02/25 Javascript
前端框架Vue.js构建大型应用浅析
2016/09/12 Javascript
nodejs 实现钉钉ISV接入的加密解密方法
2017/01/16 NodeJs
JS匹配日期和时间的正则表达式示例
2017/05/12 Javascript
Vue使用Canvas绘制图片、矩形、线条、文字,下载图片
2019/04/26 Javascript
vue项目中mock.js的使用及基本用法
2019/05/22 Javascript
Vue Cli3 打包配置并自动忽略console.log语句的方法
2020/04/23 Javascript
vue实现从外部修改组件内部的变量的值
2020/07/30 Javascript
微信小程序视频弹幕发送功能的实现
2020/12/28 Javascript
[01:57]DOTA2上海特锦赛小组赛解说单车采访花絮
2016/02/27 DOTA
Python中apply函数的用法实例教程
2014/07/31 Python
Python itertools模块详解
2015/05/09 Python
python比较两个列表是否相等的方法
2015/07/28 Python
python如何定义带参数的装饰器
2018/03/20 Python
python实现C4.5决策树算法
2018/08/29 Python
朴素贝叶斯Python实例及解析
2018/11/19 Python
使用Python+wxpy 找出微信里把你删除的好友实例
2019/02/21 Python
Scrapy-Redis结合POST请求获取数据的方法示例
2019/05/07 Python
python反编译学习之字节码详解
2019/05/19 Python
filter使用python3代码进行迭代元素的实例详解
2020/12/03 Python
Artist Guitars新西兰:乐器在线商店
2017/09/17 全球购物
国际象棋商店:The Chess Store
2018/07/09 全球购物
优瑞自动咖啡机官网:Jura
2018/09/29 全球购物
葛优非诚勿扰搞笑征婚台词
2014/03/17 职场文书
观看信仰心得体会
2014/09/04 职场文书
旅游局领导班子“四风”问题对照检查材料思想汇报
2014/09/29 职场文书
python必学知识之文件操作(建议收藏)
2021/05/30 Python