tenacity icon indicating copy to clipboard operation
tenacity copied to clipboard

Retry once after x seconds if Exception

Open Pierre-Alexandre35 opened this issue 4 years ago • 1 comments

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

  • If the RateLimit Exception occurs, I want to wait for 900 seconds and try again (not from the start again, but where I stopped)

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}")

Pierre-Alexandre35 avatar Apr 01 '21 09:04 Pierre-Alexandre35

@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.

jpark712 avatar Apr 14 '21 05:04 jpark712