Python grpc超时机制代码示例

所属分类: 脚本专栏 / python 阅读数: 1155
收藏 0 赞 0 分享

工作中遇到一个问题,上游服务通过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 3中的yield from语法详解

在python 3.3里,generator新增了一个语法 yield from,这个yield from的作用是什么?语法是什么呢?下面通过这篇文章主要给大家详细介绍了Python 3中yield from语法的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

python 开发的三种运行模式详细介绍

这篇文章主要介绍了python 开发的三种运行模式详细介绍的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

一步步教你用Python实现2048小游戏

相信2048这个游戏对大家来说一定不陌生,下面这篇文章就主要给大家介绍了怎么用Python实现2048小游戏,文中通过注释与示例代码介绍的很详细,相信对大家的理解和学习具有一定的参考借鉴价值,有需要的朋友们一起来看看吧。
收藏 0 赞 0 分享

Python爬取网易云音乐上评论火爆的歌曲

最近跟着网上教程学着用python爬取问题,于是就想试着扒一扒Python爬取网易云音乐上评论火爆的歌曲,下面这篇文章就主要介绍了利用Python如何爬取网易云音乐上那些评论火爆的歌曲,需要的朋友可以参考借鉴,一起来看看吧。
收藏 0 赞 0 分享

Python 3.x 连接数据库示例(pymysql 方式)

这篇文章主要介绍了Python 3.x 连接数据库示例(pymysql 方式),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python解决汉字编码问题:Unicode Decode Error

最近在利用python读取一个含有汉字的文档时导致出现了乱码,并报出了两个错误,无奈只能上网找寻答案,后通过网友的帮助解决了这个问题,想着总结一下,下面这篇文章就主要介绍了python如何解决汉字编码问题,有需要的朋友们可以参考借鉴。
收藏 0 赞 0 分享

Python中二维列表如何获取子区域元素的组成

这篇文章主要给大家介绍了Python中二维列表是如何获取子区域元素的组成,文中给出了详细的介绍和示例代码,相信对大家的理解和学习具有一定的参考借鉴价值,有需要的朋友们下面来一起看看吧。
收藏 0 赞 0 分享

Python正则替换字符串函数re.sub用法示例

这篇文章主要介绍了Python正则替换字符串函数re.sub用法,结合实例形式分析了正则替换字符串函数re.sub的功能及简单使用方法,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python中实现迭代器(iterator)的方法示例

我们经常需要遍历一个对象中的元素,在Python中这种功能是通过迭代器来实现的。下面这篇文章主要给大家介绍了python中实现迭代器(iterator)的方法示例,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

深入理解python对json的操作总结

Json最广泛的应用是作为AJAX中web服务器和客户端的通讯的数据格式,本篇文章主要介绍了python对json的操作总结,具有一定的参考价值,有兴趣的可以了解一下。
收藏 0 赞 0 分享
查看更多