簡體   English   中英

Python捕獲超時並重復請求

[英]Python catch timeout and repeat request

我正在嘗試將Xively API與python一起使用以更新數據流,但偶爾會收到504錯誤,似乎結束了我的腳本。

如何捕獲該錯誤,更重要的是延遲並重試,以便腳本可以繼續運行並在一分鍾左右后上傳我的數據?

這是我正在上傳的塊。

    # Upload to Xivity
    api = xively.XivelyAPIClient("[MY_API_KEY")
    feed = api.feeds.get([MY_DATASTREAM_ID])
    now = datetime.datetime.utcnow()
    feed.datastreams = [xively.Datastream(id='temps', current_value=tempF, at=now)]
    feed.update()

這是我的腳本失敗時看到的錯誤記錄:

Traceback (most recent call last):
 File "C:\[My Path] \ [My_script].py", line 39, in <module>
   feed = api.feeds.get([MY_DATASTREAM_ID])
 File "C:\Python34\lib\site-packages\xively_python-0.1.0_rc2-py3.4.egg\xively\managers.py", >line 268, in get
   response.raise_for_status()
 File "C:\Python34\lib\site-packages\requests-2.3.0-py3.4.egg\requests\models.py", line 795, >in raise_for_status
   raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 504 Server Error: Gateway Time-out

謝謝,

PS我已將我的個人信息替換為[MY_INFO],但顯然正確的數據會出現在我的代碼中。

我通常為此使用裝飾器:

from functools import wraps
from requests.exceptions import HTTPError
import time

def retry(func):
    """ Call `func` with a retry.

    If `func` raises an HTTPError, sleep for 5 seconds
    and then retry.

    """
    @wraps(func)
    def wrapper(*args, **kwargs):
        try:
            ret = func(*args, **kwargs)
        except HTTPError:
            time.sleep(5)
            ret = func(*args, **kwargs)
        return ret
    return wrapper

或者,如果您想多次重試:

def retry_multi(max_retries):
    """ Retry a function `max_retries` times. """
    def retry(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            num_retries = 0 
            while num_retries <= max_retries:
                try:
                    ret = func(*args, **kwargs)
                    break
                except HTTPError:
                    if num_retries == max_retries:
                        raise
                    num_retries += 1
                    time.sleep(5)
            return ret 
        return wrapper
    return retry

然后將您的代碼放在這樣的函數中

#@retry
@retry_multi(5) # retry 5 times before giving up.
def do_call():
    # Upload to Xivity
    api = xively.XivelyAPIClient("[MY_API_KEY")
    feed = api.feeds.get([MY_DATASTREAM_ID])
    now = datetime.datetime.utcnow()
    feed.datastreams = [xively.Datastream(id='temps', current_value=tempF, at=now)]
    feed.update()

您可以在具有睡眠計時器的循環中放入try / except語句,無論您希望兩次嘗試之間等待多長時間。 像這樣:

import time

# Upload to Xivity
api = xively.XivelyAPIClient("[MY_API_KEY")
feed = api.feeds.get([MY_DATASTREAM_ID])
now = datetime.datetime.utcnow()
feed.datastreams = [xively.Datastream(id='temps', current_value=tempF, at=now)]

### Try loop
feed_updated = False
while feed_updated == False:
    try: 
        feed.update()
        feed_updated=True
    except: time.sleep(60)

編輯正如達諾指出的那樣,最好有一個更具體的除外聲明。

### Try loop
feed_updated = False
while feed_updated == False:
    try: 
        feed.update()
        feed_updated=True
    except HTTPError: time.sleep(60) ##Just needs more time.
    except: ## Otherwise, you have bigger fish to fry
        print "Unidentified Error"
        ## In such a case, there has been some other kind of error. 
        ## Not sure how you prefer this handled. 
        ## Maybe update a log file and quit, or have some kind of notification, 
        ## depending on how you are monitoring it. 

編輯一般除外聲明。

### Try loop
feed_updated = False
feed_update_count = 0
while feed_updated == False:
    try: 
        feed.update()
        feed_updated=True
    except: 
        time.sleep(60)
        feed_update_count +=1 ## Updates counter

    if feed_update_count >= 60:    ## This will exit the loop if it tries too many times
        feed.update()              ## By running the feed.update() once more,
                                   ## it should print whatever error it is hitting, and crash

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM