av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

Python grpc超時機制代碼示例

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

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

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

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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 亚色网站| 亚洲视频三区 | 夜夜躁狠狠躁日日躁av | 黄色在线观看免费 | 亚洲91av| 色婷婷丁香 | 亚洲精品xxx | 欧美不卡 | 四虎久久久| 日韩av资源 | a级片在线免费观看 | 亚洲欧美综合网 | 欧美激情成人 | 久青草视频 | 国精产品一区二区 | 91激情 | 久久精品欧美一区二区三区不卡 | 日本女人性生活视频 | 色哟哟入口国产精品 | 99福利 | 国产精品www | 亚洲成人一区二区 | 男人影院在线观看 | 国产福利视频在线观看 | 波多野结衣在线观看一区二区 | 97在线免费观看视频 | 中文字幕www | 亚欧洲精品在线视频免费观看 | 欧美国产日韩在线 | 欧美视频亚洲视频 | 国产伦精品一区二区三区视频网站 | 欧美视频久久 | 国产香蕉在线 | 午夜激情在线观看 | 日韩精品福利 | 国产永久免费视频 | a级片在线免费观看 | 中文字幕亚洲精品 | 国产精品美女在线观看 | 国产传媒一区二区 | 一级黄色网 |