Python:如果出现异常,请在一定时间后重试

问题描述

我收到 Twitter API 速率限制错误代码 429)。根据文档,我似乎在短时间内拨打了太多电话。

我试图对以下功能做的是: 如果抛出异常,我想等待 900 秒再试一次。 如果可能,我很想使用 tenacity 库中的 @retry

这是我目前所拥有的,但 @retry 似乎不起作用,因为我仍然收到 rateLimit 错误

@retry(stop=stop_after_delay(900)) 
def get_cards_report(self):
    """
    HHH
    Get 'ENTITY' report through the 'Creatives' endpoint of Twitter Ads API.
    Supported entities: CARD
    Documentation: https://developer.twitter.com/en/docs/ads/creatives/api-reference/
    """
    try:
        for tweet in self.get_published_tweets():
            if "card_uri" in tweet:
                card_fetch = self.get_card_fetch(card_uri=tweet["card_uri"])
                card_attributes = {attr: getattr(card_fetch,attr,None) for attr in self.entity_attributes}
                record = {
                    "tweet_id": tweet["tweet_id"],"card_uri": tweet["card_uri"],**card_attributes,}
                yield record
    except RateLimit as e:
        raise Exception(f"Twitter Rate Limit exceeded: {e}")

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)