Python grpc超时机制代码示例

 更新时间:2020年09月14日 11:03:53   作者:冷冰若水  
这篇文章主要介绍了Python grpc超时机制代码示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

工作中遇到一个问题,上游服务通过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项目整洁度使用import linter实例探究

    提升Python项目整洁度使用import linter实例探究

    在复杂的Python项目中,良好的代码组织结构是维护性和可读性的关键,本文将深入研究 import-linter 工具,它是一个强大的静态分析工具,旨在优化项目的模块导入,提高代码质量和可维护性
    2024-01-01
  • Selenium 滚动页面至元素可见的方法

    Selenium 滚动页面至元素可见的方法

    这篇文章主要介绍了Selenium 滚动页面至元素可见的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-03-03
  • Python中max函数用于二维列表的实例

    Python中max函数用于二维列表的实例

    下面小编就为大家分享一篇Python中max函数用于二维列表的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-04-04
  • 使用python opencv对目录下图片进行去重的方法

    使用python opencv对目录下图片进行去重的方法

    今天小编就为大家分享一篇使用python opencv对目录下图片进行去重的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-01-01
  • Python高效解析和操作XML/HTML的实用指南

    Python高效解析和操作XML/HTML的实用指南

    在 Python 生态系统中,lxml 是一个功能强大且广泛使用的库,用于高效地解析和操作 XML 和 HTML 文档,这篇文章从 lxml 的基础安装开始,逐步深入讲解如何解析文档、提取数据、修改文档结构,并涵盖了处理大型文档和使用命名空间等进阶操作,需要的朋友可以参考下
    2024-10-10
  • 一文解密Python中的垃圾回收

    一文解密Python中的垃圾回收

    我们知道,python 是一种高级编程语言,它提供了自动内存管理的功能,即垃圾回收机制,所以本文就来聊聊python的垃圾回收机制是如何实现的以及具体是使用,感兴趣的可以了解下
    2023-09-09
  • python使用pika库调用rabbitmq参数使用详情

    python使用pika库调用rabbitmq参数使用详情

    这篇文章主要介绍了python使用pika库调用rabbitmq参数使用详情,文章通过展开文章主题分享了三种方式,具有一定的参考价值,需要的朋友可以参考一下
    2022-08-08
  • python jinja2模板的使用示例

    python jinja2模板的使用示例

    这篇文章主要介绍了python jinja2模板的使用示例,帮助大家更好的理解和学习使用python,感兴趣的朋友可以了解下
    2021-03-03
  • Python Excel vlookup函数实现过程解析

    Python Excel vlookup函数实现过程解析

    这篇文章主要介绍了Python Excel vlookup函数实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • python人工智能tensorflow函数tf.assign使用方法

    python人工智能tensorflow函数tf.assign使用方法

    这篇文章主要为大家介绍了python人工智能tensorflow函数tf.assign使用方法,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05

最新评论