]> jfr.im git - z_archive/twitter.git/commitdiff
take twitter's keepalive signals into account for the timeout
authorRouxRC <redacted>
Sun, 16 Feb 2014 02:41:38 +0000 (03:41 +0100)
committerRouxRC <redacted>
Sun, 16 Feb 2014 02:41:38 +0000 (03:41 +0100)
twitter/stream.py

index 6d58dbbb491aa45593134e0989c9d040b2e1ad63..81eaaf806d1082243af9b27f517214a1b0a9a9f2 100644 (file)
@@ -105,22 +105,19 @@ class TwitterJSONIter(object):
                 res, ptr = json_decoder.raw_decode(buf)
                 buf = buf[ptr:]
                 yield wrap_response(res, self.handle.headers)
-                timer = time.time()
                 continue
             except ValueError as e:
                 if self.block: pass
                 else: yield None
             try:
                 buf = buf.lstrip()  # Remove any keep-alive delimiters to detect hangups.
-                if self.timeout:
+                if self.timeout and not buf:  # This is a non-blocking read.
                     ready_to_read = select.select([sock], [], [], self.timeout)
-                    if ready_to_read[0]:
-                        buf += recv_chunk(sock).decode('utf-8')  # This is a non-blocking read.
-                        if time.time() - timer > self.timeout:
-                            yield {'timeout': True}
-                    else: yield {'timeout': True}
-                else:
-                    buf += recv_chunk(sock).decode('utf-8')
+                    if not ready_to_read[0] and time.time() - timer > self.timeout:
+                        yield {'timeout': True}
+                        continue
+                timer = time.time()
+                buf += recv_chunk(sock).decode('utf-8')
                 if not buf and self.block:
                     yield {'hangup': True}
                     break