Redis blpop sometimes return tuple, sometime not?
<p>I use the following code to place/retrieve items in my Redis queue but I sometime get an error when decoding the json dump, because the item returned is not a tuple but the full json.</p>

<p>Here's the class :</p>

<pre><code>class RedisQueue(object):
    """Simple Queue with Redis Backend"""
    def __init__(self, namespace, redis_url='redis://127.0.0.1:6379'):
        self.__db = redis.from_url(redis_url)
        self.redis_url = redis_url
        self.namespace = namespace

    def put(self, queue, item):
        """Put item into the queue."""
        self.__db.rpush('{0}:{1}'.format(self.namespace, queue), json.dumps(item))

    def get(self, queue, block=True, timeout=None):
        """Remove and return an item from the queue.

        If optional args block is true and timeout is None (the default), block
        if necessary until an item is available."""
        key = '{0}:{1}'.format(self.namespace, queue)
        if block:
            item = self.__db.blpop(key, timeout=timeout)
        else:
            item = self.__db.lpop(key)

        if item is not None:
            try:
                item = json.loads(item[1])
            except ValueError as e:
                sys.stderr.write("[ERROR JSON (in queue)] - {1} =&gt; {0}\n".format(str(e), str(item)))
                return None

        return item
</code></pre>

<p>I sometime get an exception at :</p>

<pre><code>    if item is not None:
        try:
            item = json.loads(item[1])
        except ValueError as e:
            sys.stderr.write("[ERROR JSON (in queue)] - {1} =&gt; {0}\n".format(str(e), str(item)))
            return None
</code></pre>

<p>That says :</p>

<pre><code>[ERROR JSON (in queue)] - {"ip": null, "domain": "somedomain.com", "name": "Some user name", "contact_id": 12345, "signature": 
"6f496a4eaba2c1ea4e371ea2c4951ad92f41ddf45ff4949ffa761b0648a22e38"} =&gt; end is out of bounds
</code></pre>

<p>This is because item is the full json, so json.loads(item[₁]) causes the error. But it occurs only from time to time, not everytime. And when I manually checked the value of item, I had a tuple with the key in 0, and the value (the json string) in 1, which is expected.</p>

<p>Why, sometime, redis returns the value in item, and sometime a tuple with <code>key,value</code> ?</p>