Python grpc超時(shí)機(jī)制代碼示例
工作中遇到一個(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的Django框架中的項(xiàng)目部署技巧
這篇文章主要全面剖析了Python的Django框架的部署技巧,包括Fabric等自動(dòng)化部署和建立單元測試等方面,強(qiáng)烈推薦!需要的朋友可以參考下2015-04-04
超級(jí)實(shí)用的8個(gè)Python列表技巧
這篇文章主要介紹了實(shí)用的8個(gè)Python列表技巧,幫助大家更好的理解和學(xué)習(xí)python列表的知識(shí),感興趣的朋友可以了解下2020-08-08
Python操作mongodb數(shù)據(jù)庫進(jìn)行模糊查詢操作示例
這篇文章主要介紹了Python操作mongodb數(shù)據(jù)庫進(jìn)行模糊查詢操作,結(jié)合實(shí)例形式分析了Python連接MongoDB數(shù)據(jù)庫及使用正則表達(dá)式進(jìn)行模糊查詢的相關(guān)操作技巧,需要的朋友可以參考下2018-06-06
使用Python處理Excel文件并將數(shù)據(jù)存儲(chǔ)到PostgreSQL的方法
在日常工作中,我們經(jīng)常會(huì)遇到需要處理大量文件并將數(shù)據(jù)存儲(chǔ)至數(shù)據(jù)庫或整合到一個(gè)文件的需求,本文將向大家展示如何使用Python處理Excel文件并將數(shù)據(jù)存儲(chǔ)到PostgreSQL數(shù)據(jù)庫中,需要的朋友可以參考下2024-01-01
Python基于argparse與ConfigParser庫進(jìn)行入?yún)⒔馕雠cini parser
這篇文章主要介紹了Python基于argparse與ConfigParser庫進(jìn)行入?yún)⒔馕雠cini parser,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-02-02
python實(shí)現(xiàn)windows壁紙定期更換功能
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)windows壁紙定期更換功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
Autopep8的使用(python自動(dòng)編排工具)
這篇文章主要介紹了Autopep8的使用(python自動(dòng)編排工具),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03

