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 相关文章推荐
使用C#配合ArcGIS Engine进行地理信息系统开发
Feb 19 Python
对numpy中向量式三目运算符详解
Oct 31 Python
python内置数据类型之列表操作
Nov 12 Python
Python判断对象是否相等及eq函数的讲解
Feb 25 Python
python实现nao机器人手臂动作控制
Apr 29 Python
Python中栈、队列与优先级队列的实现方法
Jun 30 Python
对Django 中request.get和request.post的区别详解
Aug 12 Python
Python传递参数的多种方式(小结)
Sep 18 Python
Python基础之字符串操作常用函数集合
Feb 09 Python
Python3如何判断三角形的类型
Apr 12 Python
如何基于Python爬虫爬取美团酒店信息
Nov 03 Python
虚拟环境及venv和virtualenv的区别说明
Feb 05 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
Web程序工作原理详解
2014/12/25 PHP
php使用mysqli向数据库添加数据的方法
2015/03/20 PHP
js简易namespace管理器 实例代码
2013/06/21 Javascript
jQuery+html5+css3实现圆角无刷新表单带输入验证功能代码
2015/08/21 Javascript
java必学必会之static关键字
2015/12/03 Javascript
前端学习笔记style,currentStyle,getComputedStyle的用法与区别
2016/05/28 Javascript
AngularJs Injecting Services Into Controllers详解
2016/09/02 Javascript
Vue.JS入门教程之自定义指令
2016/12/08 Javascript
搭建简单的nodejs http服务器详解
2017/03/09 NodeJs
轻松理解JavaScript之AJAX
2017/03/15 Javascript
jQuery实现基本隐藏与显示效果的方法详解
2018/09/05 jQuery
VUE DEMO之模拟登录个人中心页面之间数据传值实例
2019/10/31 Javascript
详解Vue后台管理系统开发日常总结(组件PageHeader)
2019/11/01 Javascript
Node.js API详解之 Error模块用法实例分析
2020/05/14 Javascript
JavaScript中如何调用Java方法
2020/09/16 Javascript
jQuery实现推拉门效果
2020/10/19 jQuery
[43:48]Ti4正赛第一天 VG vs NEWBEE 2
2014/07/19 DOTA
python下函数参数的传递(参数带星号的说明)
2010/09/19 Python
Python的lambda匿名函数的简单介绍
2013/04/25 Python
Python设计足球联赛赛程表程序的思路与简单实现示例
2016/06/28 Python
Windows下的Jupyter Notebook 安装与自定义启动(图文详解)
2018/02/21 Python
python调用API实现智能回复机器人
2018/04/10 Python
python3.6、opencv安装环境搭建过程(图文教程)
2019/11/05 Python
Python基于Tkinter编写crc校验工具
2020/05/06 Python
aws 通过boto3 python脚本打pach的实现方法
2020/05/10 Python
Python 执行矩阵与线性代数运算
2020/08/01 Python
一道SQL面试题
2012/12/31 面试题
办公室文秘自我评价
2013/09/21 职场文书
《彭德怀和他的大黑骡子》教学反思
2014/04/12 职场文书
职业规划实施方案
2014/06/10 职场文书
全国优秀教师事迹材料
2014/08/26 职场文书
公民授权委托书
2014/10/15 职场文书
大学生入党自传2015
2015/06/26 职场文书
关爱空巢老人感想
2015/08/11 职场文书
《伯牙绝弦》教学反思
2016/02/16 职场文书
golang中的struct操作
2021/11/11 Golang