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实例之wxpython中Frame使用方法
Jun 09 Python
python实现通过代理服务器访问远程url的方法
Apr 29 Python
Python之父谈Python的未来形式
Jul 01 Python
Python使用time模块实现指定时间触发器示例
May 18 Python
深入理解Python3 内置函数大全
Nov 23 Python
python使用KNN算法手写体识别
Feb 01 Python
如何修复使用 Python ORM 工具 SQLAlchemy 时的常见陷阱
Nov 19 Python
使用sklearn的cross_val_score进行交叉验证实例
Feb 28 Python
pycharm中导入模块错误时提示Try to run this command from the system terminal
Mar 26 Python
如何解决flask修改静态资源后缓存文件不能及时更改问题
Aug 02 Python
python 三种方法提取pdf中的图片
Feb 07 Python
Python基本数据类型之字符串str
Jul 21 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
CI使用Tank Auth转移数据库导致密码用户错误的解决办法
2014/06/12 PHP
CI框架中zip类应用示例
2014/06/17 PHP
php使用curl通过代理获取数据的实现方法
2016/05/16 PHP
PHP排序算法之基数排序(Radix Sort)实例详解
2018/04/21 PHP
PHP PDOStatement::fetchAll讲解
2019/01/31 PHP
可以把编码转换成 gb2312编码lib.UTF8toGB2312.js
2007/08/21 Javascript
Prototype1.6 JS 官方下载地址
2007/11/30 Javascript
js计算精度问题小结
2013/04/22 Javascript
批量实现面向对象的实例代码
2013/07/01 Javascript
用innerhtml提高页面打开速度的方法
2013/08/02 Javascript
jQuery中parents()和parent()的区别分析
2014/10/28 Javascript
JS控制按钮10秒钟后可用的方法
2015/12/22 Javascript
js cookie实现记住密码功能
2017/01/17 Javascript
浅谈Vuejs Prop基本用法
2017/08/17 Javascript
在vue中添加Echarts图表的基本使用教程
2017/11/22 Javascript
浅谈React中的元素、组件、实例和节点
2018/02/27 Javascript
对vue里函数的调用顺序介绍
2018/03/17 Javascript
vue elementUI table 自定义表头和行合并的实例代码
2019/05/22 Javascript
基于mpvue的简单弹窗组件mptoast使用详解
2019/08/02 Javascript
js函数和this用法实例分析
2020/03/13 Javascript
[02:38]DOTA2亚洲邀请赛 IG战队巡礼
2015/02/03 DOTA
Python生成随机密码
2015/03/10 Python
Python爬虫实现爬取京东手机页面的图片(实例代码)
2017/11/30 Python
Python数据分析中Groupby用法之通过字典或Series进行分组的实例
2017/12/08 Python
详解用python实现简单的遗传算法
2018/01/02 Python
Python实现PS图像调整之对比度调整功能示例
2018/01/26 Python
3种适用于Python的疯狂秘密武器及原因解析
2020/04/29 Python
Python Django form 组件动态从数据库取choices数据实例
2020/05/19 Python
python中用ctypes模拟点击的实例讲解
2020/11/26 Python
Grow Gorgeous美国官网:只要八天,体验唤醒毛囊后新生的茂密秀发
2018/06/04 全球购物
艺术爱好者的自我评价分享
2013/10/08 职场文书
小区门卫工作职责
2013/12/14 职场文书
办公室主任主任岗位责任制
2014/02/11 职场文书
2015年护士工作总结范文
2015/03/31 职场文书
22句经典语录:送给优柔寡断和胡思乱想的朋友们
2019/12/13 职场文书
Golang Elasticsearches 批量修改查询及发送MQ
2022/04/19 Golang