Python抓取超时并重复请求

2024-06-16 13:27:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用xivelyapi和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()

下面是脚本失败时记录的错误:

^{pr2}$

谢谢

很明显我的个人信息被我的个人信息代替了。在


Tags: to数据脚本apidatetimemyfeed地方
2条回答

您可以在一个循环中抛出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)

编辑正如Dano指出的,最好有一个更具体的except语句。在

^{pr2}$

编辑一个通用的except语句。在

### 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

我通常用装饰师来做这个:

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

或者,如果要多次重试:

^{pr2}$

像这样把你的代码放进去

#@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()

相关问题 更多 >