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检测字符串中是否包含某字符集合中的字符
May 21 Python
深入解析Python设计模式编程中建造者模式的使用
Mar 02 Python
Python Xml文件添加字节属性的方法
Mar 31 Python
Python实现按照指定要求逆序输出一个数字的方法
Apr 19 Python
python 查找文件名包含指定字符串的方法
Jun 05 Python
Python 从列表中取值和取索引的方法
Dec 25 Python
浅谈python 中类属性共享的问题
Jul 02 Python
Python实现代码统计工具
Sep 19 Python
python反转列表的三种方式解析
Nov 08 Python
Python3 把一个列表按指定数目分成多个列表的方式
Dec 25 Python
Python函数的定义方式与函数参数问题实例分析
Dec 26 Python
Pytorch中实现只导入部分模型参数的方式
Jan 02 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工厂模式简单实现方法示例
2018/05/23 PHP
PHP字符串中抽取子串操作实例分析
2019/06/22 PHP
php使用gearman进行任务分发操作实例详解
2020/02/26 PHP
javascript动态加载实现方法一
2012/08/22 Javascript
Jquery UI震动效果实现原理及步骤
2013/02/04 Javascript
js和php如何获取当前url的内容
2013/09/22 Javascript
JavaScript中for-in遍历方式示例介绍
2014/02/11 Javascript
页面刷新时记住滚动条的位置jquery代码
2014/06/17 Javascript
javascript编程异常处理实例小结
2015/11/30 Javascript
Bootstrap分页插件之Bootstrap Paginator实例详解
2016/10/15 Javascript
JS出现失效的情况总结
2017/01/20 Javascript
利用Three.js如何实现阴影效果实例代码
2017/09/26 Javascript
JS中Object对象的原型概念基础
2018/01/29 Javascript
Vue.js中的computed工作原理
2018/03/22 Javascript
vue+springmvc导出excel数据的实现代码
2018/06/27 Javascript
bootstrap下拉框动态赋值方法
2018/08/10 Javascript
详解vue中axios的使用与封装
2019/03/20 Javascript
Vue项目部署的实现(阿里云+Nginx代理+PM2)
2019/03/26 Javascript
浅谈react-router@4.0 使用方法和源码分析
2019/06/04 Javascript
js简单遍历获取对象中的属性值的方法示例
2019/06/19 Javascript
PHP读取远程txt文档到数组并实现遍历
2020/08/25 Javascript
vue v-model的用法解析
2020/10/19 Javascript
node脚手架搭建服务器实现token验证的方法
2021/01/20 Javascript
[02:47]3.19DOTA2发布会 国服成长历程回顾
2014/03/25 DOTA
Python3搜索及替换文件中文本的方法
2015/05/22 Python
浅谈Python 函数式编程
2020/06/20 Python
快速解决pymongo操作mongodb的时区问题
2020/12/05 Python
当x.equals(y)等于true时,x.hashCode()与y.hashCode()可以不相等,这句话对不对
2015/05/02 面试题
关于期中考试的反思
2014/02/02 职场文书
高中物理教学反思
2014/02/08 职场文书
捐款感谢信
2015/01/20 职场文书
2015年挂职锻炼个人总结
2015/10/22 职场文书
中国式结婚:司仪主持词(范文)
2019/07/25 职场文书
详细谈谈MYSQL中的COLLATE是什么
2021/06/11 MySQL
vue封装数字翻牌器
2022/04/20 Vue.js
一文了解Java动态代理的原理及实现
2022/07/07 Java/Android