Skip to content

Commit

Permalink
Added better error handling and idle connection timeouts.
Browse files Browse the repository at this point in the history
  • Loading branch information
joshthecoder committed Aug 8, 2009
1 parent 0c3441b commit f266256
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions tweepy/streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@

class Stream(object):

def __init__(self, username, password, callback, host='stream.twitter.com', timeout=2.0, buffer_size=1500):
self.host = host
host = 'stream.twitter.com'

def __init__(self, username, password, callback, timeout=2.0, retry_count = 3,
retry_time = 5.0, snooze_time = 10.0, buffer_size=1500):
self.auth = BasicAuthHandler(username, password)
self.running = False
self.timeout = timeout
self.retry_count = retry_count
self.retry_time = retry_time
self.snooze_time = snooze_time
self.buffer_size = buffer_size
self.callback = callback
self.api = API()
Expand All @@ -34,26 +39,37 @@ def _run(self):
self.auth.apply_auth(None, None, headers, None)

# enter loop
error_counter = 0
conn = None
while self.running:
if error_counter > self.retry_count:
# quit if error count greater than retry count
break
try:
conn = httplib.HTTPConnection(self.host, timeout=self.timeout)
conn.request('POST', self.url, headers=headers)
resp = conn.getresponse()
if resp.status != 200:
# TODO: better handle failures
sleep(5.0)
continue
self._read_loop(resp)
error_counter += 1
sleep(self.retry_time)
else:
error_counter = 0
self._read_loop(resp)
except timeout:
if self.running is False:
break
conn.close()
continue
print 'snoozing...'
sleep(self.snooze_time)
print 'ok im awake!'
except Exception:
# any other exception is fatal, so kill loop
self.running = False
break

# cleanup
conn.close()
self.running = False
if conn:
conn.close()

def _read_loop(self, resp):
data = ''
Expand Down

0 comments on commit f266256

Please sign in to comment.