Use a buffer rather than reading the Twitter response one byte at a time. This actual...
authorDan Fairs <dan@fezconsulting.com>
Tue, 31 Jan 2012 15:47:38 +0000 (15:47 +0000)
committerDan Fairs <dan@fezconsulting.com>
Tue, 31 Jan 2012 15:47:38 +0000 (15:47 +0000)
tweepy/streaming.py

index 2e5c7e80675162d758e8463a3903b20a77d11111..60b043c2dd25d16d60ff6b360c9c48072c6c78cc 100644 (file)
@@ -135,23 +135,31 @@ class Stream(object):
         if exception:
             raise
 
+    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):
-          while self.running:
+        while self.running:
             if resp.isclosed():
                 break
 
-            # read length
-            data = ''
+            buf = ''
             while True:
-                c = resp.read(1)
-                if c == '\n':
-                    break
-                data += c
-            data = data.strip()
-
-            # read data and pass into listener
-            if self.listener.on_data(data) is False:
-                self.running = False
+                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