最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python grpc超時(shí)機(jī)制代碼示例

 更新時(shí)間:2020年09月14日 11:03:53   作者:冷冰若水  
這篇文章主要介紹了Python grpc超時(shí)機(jī)制代碼示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

工作中遇到一個(gè)問題,上游服務(wù)通過grpc調(diào)用下游服務(wù),但是由于下游服務(wù)負(fù)載太高導(dǎo)致上游服務(wù)的調(diào)用會(huì)隨機(jī)出現(xiàn)超時(shí)的情況,但是有一點(diǎn)不太明確:超時(shí)之后,下游服務(wù)還會(huì)繼續(xù)進(jìn)行計(jì)算么?

于是自己寫了一個(gè)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()

這兩個(gè)例子就是在grpc官方提供的python例子上做了一下小的改動(dòng),得到的結(jié)果是:當(dāng)client超時(shí)報(bào)錯(cuò)退出之后,server還是會(huì)繼續(xù)進(jìn)行計(jì)算,直到結(jié)束,那如果是這樣的話,超時(shí)的機(jī)制對(duì)于server來說是沒有作用的,即使client已經(jīng)不再等待這個(gè)結(jié)果了,但是server還是會(huì)繼續(xù)計(jì)算,浪費(fèi)server的資源。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 提升Python項(xiàng)目整潔度使用import?linter實(shí)例探究

    提升Python項(xiàng)目整潔度使用import?linter實(shí)例探究

    在復(fù)雜的Python項(xiàng)目中,良好的代碼組織結(jié)構(gòu)是維護(hù)性和可讀性的關(guān)鍵,本文將深入研究?import-linter?工具,它是一個(gè)強(qiáng)大的靜態(tài)分析工具,旨在優(yōu)化項(xiàng)目的模塊導(dǎo)入,提高代碼質(zhì)量和可維護(hù)性
    2024-01-01
  • Selenium 滾動(dòng)頁面至元素可見的方法

    Selenium 滾動(dòng)頁面至元素可見的方法

    這篇文章主要介紹了Selenium 滾動(dòng)頁面至元素可見的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • Python中max函數(shù)用于二維列表的實(shí)例

    Python中max函數(shù)用于二維列表的實(shí)例

    下面小編就為大家分享一篇Python中max函數(shù)用于二維列表的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • 使用python opencv對(duì)目錄下圖片進(jìn)行去重的方法

    使用python opencv對(duì)目錄下圖片進(jìn)行去重的方法

    今天小編就為大家分享一篇使用python opencv對(duì)目錄下圖片進(jìn)行去重的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python高效解析和操作XML/HTML的實(shí)用指南

    Python高效解析和操作XML/HTML的實(shí)用指南

    在?Python?生態(tài)系統(tǒng)中,lxml?是一個(gè)功能強(qiáng)大且廣泛使用的庫,用于高效地解析和操作?XML?和?HTML?文檔,這篇文章從?lxml?的基礎(chǔ)安裝開始,逐步深入講解如何解析文檔、提取數(shù)據(jù)、修改文檔結(jié)構(gòu),并涵蓋了處理大型文檔和使用命名空間等進(jìn)階操作,需要的朋友可以參考下
    2024-10-10
  • 一文解密Python中的垃圾回收

    一文解密Python中的垃圾回收

    我們知道,python?是一種高級(jí)編程語言,它提供了自動(dòng)內(nèi)存管理的功能,即垃圾回收機(jī)制,所以本文就來聊聊python的垃圾回收機(jī)制是如何實(shí)現(xiàn)的以及具體是使用,感興趣的可以了解下
    2023-09-09
  • python使用pika庫調(diào)用rabbitmq參數(shù)使用詳情

    python使用pika庫調(diào)用rabbitmq參數(shù)使用詳情

    這篇文章主要介紹了python使用pika庫調(diào)用rabbitmq參數(shù)使用詳情,文章通過展開文章主題分享了三種方式,具有一定的參考價(jià)值,需要的朋友可以參考一下
    2022-08-08
  • python jinja2模板的使用示例

    python jinja2模板的使用示例

    這篇文章主要介紹了python jinja2模板的使用示例,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-03-03
  • Python Excel vlookup函數(shù)實(shí)現(xiàn)過程解析

    Python Excel vlookup函數(shù)實(shí)現(xiàn)過程解析

    這篇文章主要介紹了Python Excel vlookup函數(shù)實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • python人工智能tensorflow函數(shù)tf.assign使用方法

    python人工智能tensorflow函數(shù)tf.assign使用方法

    這篇文章主要為大家介紹了python人工智能tensorflow函數(shù)tf.assign使用方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05

最新評(píng)論

大悟县| 百色市| 太和县| 新巴尔虎右旗| 丰县| 柳江县| 门源| 革吉县| 北京市| 南宫市| 齐河县| 简阳市| 万宁市| 长垣县| 桐梓县| 得荣县| 遂昌县| 望奎县| 读书| 吴堡县| 什邡市| 榆树市| 洛川县| 喜德县| 闵行区| 丽江市| 莱芜市| 东辽县| 公安县| 靖宇县| 祁阳县| 新巴尔虎右旗| 逊克县| 息烽县| 镇宁| 嵊州市| 临潭县| 通许县| 当雄县| 林口县| 漳浦县|