Commit a3705581 authored by Joshua Roesslein's avatar Joshua Roesslein
Browse files

Merge pull request #144 from danfairs/faster-read-loop

Use a buffer rather than reading the Twitter response one byte at a time...
parents 41fe5dc5 4b48cb9e
......@@ -135,24 +135,31 @@ class Stream(object):
if exception:
def _read_loop(self, resp):
while self.running:
if resp.isclosed():
# read length
data = ''
while True:
c =
if c == '\n':
data += c
data = data.strip()
# read data and pass into listener
if self.listener.on_data(data) is False:
def _data(self, data):
for d in [dt for dt in data.split('\n') if dt]:
if self.listener.on_data(d) is False:
self.running = False
def _read_loop(self, resp):
buf = ''
while self.running and not resp.isclosed():
c =
idx = c.rfind('\n')
if idx > -1:
# There is an index. Store the tail part for later,
# and process the head part as messages. We use idx + 1
# as we dont' actually want to store the newline.
data = buf + c[:idx]
buf = c[idx + 1:]
# No newline found, so we add this to our accumulated
# buffer
buf += c
if resp.isclosed():
def _start(self, async):
self.running = True
if async:
......@@ -160,6 +167,10 @@ class Stream(object):
def on_closed(self, resp):
""" Called when the response has been closed by Twitter """
def userstream(self, count=None, async=False, secure=True):
if self.running:
raise TweepError('Stream object already connected!')
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment