From 5720a0b1c1702d237ca4b7deba4e807941978085 Mon Sep 17 00:00:00 2001 From: Josh Roesslein Date: Tue, 13 Oct 2009 18:18:30 -0500 Subject: [PATCH] Update tutorial 4 to demo retry request feature. --- CHANGES | 1 + tutorial/t4.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/CHANGES b/CHANGES index 6cf3e56..c060a7f 100644 --- a/CHANGES +++ b/CHANGES @@ -7,6 +7,7 @@ during upgrade will be listed here. + Added automatic request re-try feature Example: API.friends(retry_count=5, retry_delay=10) Retry up to 5 times with a delay of 10 seconds between each attempt. + See tutorial/t4.py for more an example. + Added cursor parameter to API.friends and API.followers methods. Note: page parameter is being deprecated by twitter on 10/26 + Update parsing to handle cursor responses. diff --git a/tutorial/t4.py b/tutorial/t4.py index dd4feb1..9fb3688 100644 --- a/tutorial/t4.py +++ b/tutorial/t4.py @@ -32,5 +32,33 @@ The main reasons an exception will be raised include: Be sure to keep a look out for these exceptions and handle them properly. """ +""" +If Twitter returns an error response you may wish to retry the request +again. To help make this easier Tweepy allows you to configure it to do +this for you automatically when such error codes are returned. + +Here is an example of performing a request and retrying if it fails. +We will tell Tweepy to only attempt up to 5 retries and wait 5 seconds +between each attempt. +""" +try: + tweepy.api.friends_timeline(retry_count=5, retry_delay=5) +except tweepy.TweepError, e: + # If all 5 attempts fail a TweepError will be thrown + print 'Failed to get timeline: %s' % e + +""" +By default Tweepy will only retry when a status code of 500, 502, or 503 +is returned by Twitter. All other codes result in an immediate TweepError. +If we do not provide a retry_delay Tweepy by default does not wait between requests. + +Let's say we want to retry on status code responses of 400 only. +Here is how we do that... +""" +try: + tweepy.api.user_timeline('twitter', retry_count=3, retry_delay=5, retry_errors=[400]) +except tweepy.TweepError, e: + print 'Failed to get timeline: %s' % e + """ The End """ -- 2.25.1