jd / tenacity

Retrying library for Python
http://tenacity.readthedocs.io
Apache License 2.0
6.69k stars 281 forks source link

Retry once after x seconds if Exception #287

Open Pierre-Alexandre35 opened 3 years ago

Pierre-Alexandre35 commented 3 years ago

I am trying to avoid a RateLimit Exception on my function and what I a trying to achieve is:

This is my current code but I am still getting the Exception at the same time exact moment as before, so there is no waiting period. Do you have any idea how can I fix it?

@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}")
jpark712 commented 3 years ago

@Pierre-Alexandre35 , In the example you have, you would keep retrying without a delay and then stop after 900 seconds. I think you want to wait for 900 seconds after a failure, so try something like this:

@retry(wait=wait_fixed(900))
def get_cards_report(self):
   ...

This will retry after 900 seconds for any exception. If you only want to retry when the raised exception ("Twitter Rate Limit exceeded") occurs, you can use the retry_if_exception_message with a match type as an argument to the retry decorator.