Package: saferedisqueue
saferedisqueueA small wrapper aroundRedis(using the “standard”Python redis client lib) that provides
access to a FIFO queue and allows upstream code to mark pop’ed items as
processed successfully (ACK’ing) or unsucessfully (FAIL’ing).Failed items are automatically requeued. Addionally a backup is kept for items
that were neither ack’ed nor fail’ed, i.e. in case the consumer crashes. The
backup items will be requeued as soon as one of the consumer(s) comes up
again.All actions are atomic and it is safe to have multiple producers and consumers
accessing the same queue concurrently.Version compatibility with redis.pyredis.pysaferedisqueue2.4.10 - 2.6.x1.x2.7.0 - 2.7.5no compatible version2.7.6 - 2.10.x2.x-3.xUsage as a library>>> queue = SafeRedisQueue(name='test')
>>> queue.put("Hello World")
'595d43b2-2e49-4e96-a1d2-0848d1c7f0d3'
>>> queue.put("Foo bar")
'1df060eb-b578-499d-bede-20db9da8184e'
>>> queue.get()
('595d43b2-2e49-4e96-a1d2-0848d1c7f0d3', 'Hello World')
>>> queue.get()
('1df060eb-b578-499d-bede-20db9da8184e', 'Foo bar')Note: to be compatible with previous versions, 2 aliasespush/popexist. Start using the newput/getterminology as soon as possible sincepush/popwill be deleted in a future version.ACK’ing and FAIL’ing>>> queue.put("Good stuff")
>>> queue.put("Bad stuff")
>>> uid_good, payload_good = queue.get()
>>> uid_bad, payload_bad = queue.get()
...
# process the payloads...
...
>>> queue.ack(uid_good)  # done with that one
>>> queue.fail(uid_bad)  # something didn't work out with that one, let's requeue
>>> uid, payload = queue.get()  # get again; we get the requeued payload again
>>> uid == uid_bad
True
...
# try again
...
>>> queue.ack(uid)  # now it worked; ACK the stuff nowget timeoutSafeRedisQueue.getaccepts a timeout parameter:0 (the default) blocks forever, waiting for itemsa positive number blocks that amount of secondsa negative timeout disables blockingConstructor parametersSafeRedisQueueaccepts*args, **kwargsand passes them toredis.StrictRedis, so use whatever you need.Three exceptions, use these in the keyword arguments to configureSafeRedisQueueitself:urlShortcut to use instead of a host/port/db/password combinations.
Accepts “redis URLs” just as the redis library does, for example:redis://[:password]@localhost:6379/0unix://[:password]@/path/to/socket.sock?db=0When using this keyword parameter, all positional arguments (usually
one the host) are ignored. Those two are equivalent:SafeRedisQueue('localhost',port=6379, db=7)SafeRedisQueue(url='redis://localhost:6379/7')nameA prefix used for the keys in Redis. Default: “0”, which creates the
following keys in your Redis DB:srq:0:itemssrq:0:queuesrq:0:ackbufsrq:0:backupsrq:0:backuplockautoclean_intervalAn interval in seconds (default: 60) at whichunacknowledgeditems are
requeued automatically. (They are moved from the internal ackbuf and backup data
structures to the queue again.)PassNoneto disable autocleaning. It’s enabled by default!serializerAn optional serializer to use on the items. Default: NoneFeel free to write your own serializer. It only requires adumpsandloadsmethods. Modules likepickle,json,simplejsoncan be used out of the box.Note however, that when using Python 3 thejsonmodule has to be
wrapped as it by default does not handle thebytesproperly that
is emitted by the underlying redis.py networking code. This should
work:class MyJSONSerializer(object):
    @staticmethod
    def loads(bytes):
        return json.loads(bytes.decode('utf-8'))

    @staticmethod
    def dumps(data):
        return json.dumps(data)

queue = SafeRedisQueue(name='foobar',serializer=MyJSONSerializer)Added in version 3.0.0Command line usageFor quick’n’dirty testing, you can use the script from the command line to put stuff into the queue:$ echo "Hello World" | python saferedisqueue.py producer…and get it out again:$ python saferedisqueue.py consumer
cbdabbc8-1c0f-4eb0-8733-fdb62a9c0fa6 Hello WorldChanges3.0.0 - 2014-11-13BREAKING CHANGE (very unlikely, though):push/putnow returns the
uid of the just added item. Before it always returnedNone.pushhas been renamed toput;pushstays as an alias, but is
deprecated and will be removed in version 4.pophas been renamed toget;popstays as an alias, but is
deprecated and will be removed in version 4.Increased version compatibility with redis.py to “<2.11”.Python 3.4 supportMore tests. Introduced tox for testing.Addedserializerparameter to support custom serializers,
e.g. for automatic JSON conversion. See README for details, especially
when using Python 3.2.0.0 - 2014-06-26SafeRedisQueue is now officially compatible with recent versions
(roughly speaking 2.7-2.10) of redis.py.For versions 2.4 and 2.6 please continue using the 1.x development
line.See README.rst for details on compatibility.1.2.0 - 2014-06-26Raise compatible redis.py version up to 2.6.x. Updated README with
compatibility details.1.1.0 - 2014-06-20The constructor now accepts a “url” keyword parameter that supports
typical redis URL a la “redis://[:password]@localhost:6379/0”. See
README for details.1.0.1 - 2013-06-26Changed dependency on redis to require at least version 2.4.10 as
redis.StrictRedis, which we use, was only introduced in that version.
This should not affect anyone negatively as you wouldn’t have to be able
to use saferedisqueue at all if your project or package used an older
version so far.
(Thanks Telofy!)1.0.0 - 2013-06-05Released as open-source under 3-clause BSD license. Code identical to 0.3.0.0.3.0 - 2012-05-22The constructor now accepts an “autoclean_interval” value to set the interval
at which the ackbuf -> backup rotation and backup requeuing happens.
Setting the value toNonedisables autoclean completely.
Default: 60 (seconds).0.2.2 - 2012-03-29Negative timeout makes .pop() non-blocking.0.2.1 - 2012-03-09Added setup.py and publish it on our internal package directory.0.2.0 - 2012-03-08Renamed methods (“push_item” -> “push” etc.)New autoclean method that is called on every .pop()Internal: New names for keys in the redis db.