From 15f62d34ead34c3dc0f946d675eae434634c928a Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 24 Apr 2009 21:33:04 -0500 Subject: [PATCH 1/1] Found the problem with the randomly bad data returned by RTM, read seems to be non-blocking and so causes issues on longer items --- src/rtm_api.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/rtm_api.py b/src/rtm_api.py index b617632..720c0d2 100644 --- a/src/rtm_api.py +++ b/src/rtm_api.py @@ -3,8 +3,6 @@ Python library for Remember The Milk API @note For help, see http://www.rememberthemilk.com/services/api/methods/ - -@bug Under random situations, the response comes back incomplete. Maybe a race condition, but how? """ import weakref @@ -12,6 +10,7 @@ import warnings import urllib import urllib2 import hashlib +import time _use_simplejson = False try: @@ -99,7 +98,24 @@ class RTMapi(object): params['api_sig'] = self._sign(params) connection = self.open_url(SERVICE_URL, params) - json = connection.read() + + # It appears that urllib uses the non-blocking variant of file objects + # which means reads might not always be complete, so grabbing as much + # of the data as possible with a sleep in between to give it more time + # to grab data. + chunks = [] + chunk = connection.read() + while chunk: + chunks.append(chunk) + time.sleep(1) + chunk = connection.read() + json = "".join(chunks) + if "Content-Length" in connection.info(): + assert len(json) == int(connection.info()["Content-Length"]), "The packet header promised %s of data but only was able to read %s of data" % ( + len(json), + connection.info()["Content-Length"], + ) + data = DottedDict('ROOT', parse_json(json)) rsp = data.rsp -- 1.7.9.5