Python grpc超時機制代碼示例
工作中遇到一個問題,上游服務(wù)通過grpc調(diào)用下游服務(wù),但是由于下游服務(wù)負(fù)載太高導(dǎo)致上游服務(wù)的調(diào)用會隨機出現(xiàn)超時的情況,但是有一點不太明確:超時之后,下游服務(wù)還會繼續(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_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例子上做了一下小的改動,得到的結(jié)果是:當(dāng)client超時報錯退出之后,server還是會繼續(xù)進行計算,直到結(jié)束,那如果是這樣的話,超時的機制對于server來說是沒有作用的,即使client已經(jīng)不再等待這個結(jié)果了,但是server還是會繼續(xù)計算,浪費server的資源。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
提升Python項目整潔度使用import?linter實例探究
在復(fù)雜的Python項目中,良好的代碼組織結(jié)構(gòu)是維護性和可讀性的關(guān)鍵,本文將深入研究?import-linter?工具,它是一個強大的靜態(tài)分析工具,旨在優(yōu)化項目的模塊導(dǎo)入,提高代碼質(zhì)量和可維護性2024-01-01
python使用pika庫調(diào)用rabbitmq參數(shù)使用詳情
這篇文章主要介紹了python使用pika庫調(diào)用rabbitmq參數(shù)使用詳情,文章通過展開文章主題分享了三種方式,具有一定的參考價值,需要的朋友可以參考一下2022-08-08
Python Excel vlookup函數(shù)實現(xiàn)過程解析
這篇文章主要介紹了Python Excel vlookup函數(shù)實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06
python人工智能tensorflow函數(shù)tf.assign使用方法
這篇文章主要為大家介紹了python人工智能tensorflow函數(shù)tf.assign使用方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05

