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 相关文章推荐
在Python3中使用asyncio库进行快速数据抓取的教程
Apr 02 Python
Django中URLconf和include()的协同工作方法
Jul 20 Python
浅谈python中requests模块导入的问题
May 18 Python
python3将视频流保存为本地视频文件
Jun 20 Python
解决python报错MemoryError的问题
Jun 26 Python
django2+uwsgi+nginx上线部署到服务器Ubuntu16.04
Jun 26 Python
pytorch训练imagenet分类的方法
Jul 27 Python
python实现批量注册网站用户的示例
Feb 22 Python
python判断自身是否正在运行的方法
Aug 08 Python
python实现飞机大战小游戏
Nov 08 Python
pandas参数设置的实用小技巧
Aug 23 Python
Python 实现键盘鼠标按键模拟
Nov 18 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
ajax完美实现两个网页 分页功能的实例代码
2013/04/16 PHP
ThinkPHP模板比较标签用法详解
2014/06/30 PHP
PHP7导出Excel报ERR_EMPTY_RESPONSE解决方法
2019/04/16 PHP
JavaScript 事件查询综合
2009/07/13 Javascript
javascript提取URL的搜索字符串中的参数(自定义函数实现)
2013/01/22 Javascript
JS前端框架关于重构的失败经验分享
2013/03/17 Javascript
浮动的div自适应居中显示的js代码
2013/12/23 Javascript
学习javascript面向对象 实例讲解面向对象选项卡
2016/01/04 Javascript
微信小程序 页面传值详解
2017/03/10 Javascript
JavaScript限定范围拖拽及自定义滚动条应用(3)
2017/05/17 Javascript
Angular实现类似博客评论的递归显示及获取回复评论的数据
2017/11/06 Javascript
jQuery插件实现非常实用的tab栏切换功能【案例】
2019/02/18 jQuery
微信小程序select下拉框实现效果
2019/05/15 Javascript
Python实现命令行通讯录实例教程
2016/08/18 Python
python实现京东秒杀功能
2018/07/30 Python
python中resample函数实现重采样和降采样代码
2020/02/25 Python
CSS3 3D位移translate效果实例介绍
2016/05/03 HTML / CSS
使用css创建三角形 使用CSS3创建3d四面体原理及代码(html5实践)
2013/01/06 HTML / CSS
英国的潮牌鞋履服饰商店:size?
2019/03/26 全球购物
维多利亚的秘密阿联酋官网:Victoria’s Secret阿联酋
2019/12/07 全球购物
如何写出高质量、高性能的MySQL查询
2014/11/17 面试题
Unix如何在一行中运行多个命令
2015/05/29 面试题
学习委员自我鉴定
2014/01/13 职场文书
求职信内容怎么写
2014/05/26 职场文书
课例研修方案
2014/05/31 职场文书
服务员岗位职责
2015/02/03 职场文书
单身申明具结书
2015/02/26 职场文书
2015年法制宣传月活动总结
2015/03/26 职场文书
幼儿园2016年感恩节活动总结
2016/04/01 职场文书
2016年大学生暑期社会实践活动总结
2016/04/06 职场文书
创业计划书之校园超市
2019/09/12 职场文书
MySQL 如何分析查询性能
2021/05/12 MySQL
Python排序算法之插入排序及其优化方案详解
2021/06/11 Python
react使用antd的上传组件实现文件表单一起提交功能(完整代码)
2021/06/29 Javascript
详解TypeScript的基础类型
2022/02/18 Javascript
豆瓣2021评分最高动画剧集-豆瓣评分最高的动画剧集2021
2022/03/18 日漫