From c2edbea600ce072fa075b0fabe1cd49ba549efa6 Mon Sep 17 00:00:00 2001 From: Dan Fairs Date: Sat, 4 Feb 2012 16:22:34 +0000 Subject: [PATCH] Check that the response is not closed each time around the loop. --- tweepy/streaming.py | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/tweepy/streaming.py b/tweepy/streaming.py index 60b043c..6f8b4a6 100644 --- a/tweepy/streaming.py +++ b/tweepy/streaming.py @@ -141,25 +141,22 @@ class Stream(object): self.running = False def _read_loop(self, resp): - while self.running: - if resp.isclosed(): - break + buf = '' + while self.running and not resp.isclosed(): + c = resp.read(self.buffer_size) + 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:] + self._data(data) + else: + # No newline found, so we add this to our accumulated + # buffer + buf += c - buf = '' - while True: - c = resp.read(self.buffer_size) - 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:] - self._data(data) - else: - # No newline found, so we add this to our accumulated - # buffer - buf += c def _start(self, async): self.running = True -- 2.25.1