亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

瀏覽:3日期:2022-07-11 11:34:45

工作中遇到一個(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_functionimport loggingimport grpcimport helloworld_pb2import helloworld_pb2_grpcdef 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 futuresimport timeimport loggingimport grpcimport helloworld_pb2import helloworld_pb2_grpc_ONE_DAY_IN_SECONDS = 60 * 60 * 24class 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來(lái)說(shuō)是沒有作用的,即使client已經(jīng)不再等待這個(gè)結(jié)果了,但是server還是會(huì)繼續(xù)計(jì)算,浪費(fèi)server的資源。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: jdav视频在线观看 | 成人午夜免费观看 | 亚洲国产精品午夜电影 | 草草免费观看视频在线 | 免费国产成人手机在线观看 | 日本一级毛片视频 | 国产无内制服肉丝精品视频 | 日韩欧美一区二区三区不卡视频 | 国产欧美日本在线观看 | 特大巨黑人吊与黑人性xxxx | 欧美精品aaa久久久影院 | 精品亚洲成a人在线播放 | 欧美一级毛片不卡免费观看 | 日本一级特黄a大片在线 | 色偷偷亚洲女人天堂观看欧 | 成人最新午夜免费视频 | 亚洲综合久久成人69 | 欧美在线视频一区二区三区 | 国产免费一区二区三区在线观看 | 有码日韩 | 国产在线观看91精品2022 | 丰满的日本护士xxx 丰满美女福利视频在线播放 | 成人国产三级精品 | 成人精品mv视频在线观看 | 久草久热 | 国产精品青草久久久久福利99 | 综合玖玖 | 国产精品亚洲精品爽爽 | 老师xxxxbbbb| 国模午夜写真福利视频在线 | 黑人操日本人视频 | 欧美一级黄色片 | 欧美一区二区三区精品 | 一本高清在线视频 | 五月六月婷婷 | 黄色一级一毛片 | 中文字幕日韩欧美一区二区三区 | 韩国理论毛片a级 | 青青草精品在线视频 | 欧美人与善交大片 | 在线观看免费网址大全 |