Redset offers a narrow interface consisting of a few objects. Most often, you’ll be using an object that resembles a set.
There are two interesting components to the sets in redset: scorers and serializers.
Scorers determine what score the inserted item will be assigned (lower means popped sooner). Serializers determine what transformations happen on an item going into and coming out of redis.
Note
If an Exception is thrown while deserialization is attempted on a particular item, None will be returned in its stead and an exception will be logged. In the case of SortedSet.take(), the failed item will simply be filtered from the returned list.
A Redis-backed sorted set safe for multiprocess consumption.
By default, items are stored and returned as str. Scores default to 0.
A serializer can be specified to ease packing/unpacking of items. Otherwise, items are cast to and returned as strings.
Parameters: |
|
---|
How many values are the in the set?
Returns: | int |
---|
Add the item to the set. It the item is already in the set, update its score.
Parameters: |
|
---|---|
Returns: | Number – score the item was added with |
Empty the set of all scores and ID strings.
Returns: | bool |
---|
Remove a given item from the set.
Parameters: | item (object) – |
---|---|
Returns: | bool – success of removal |
The name of this set and the string that identifies the redis key where this set is stored.
Returns: | str |
---|
Return the next item eligible for processing without removing it.
Raises: | KeyError – if no items left |
---|---|
Returns: | object |
What is the score of the next item to be processed? This is interesting if you are trying to keep your set at real-time consumption.
Returns: | Number |
---|
Atomically remove and return the next item eligible for processing in the set.
If, for some reason, deserializing the object fails, None is returned and the object is deleted from redis.
Raises: | KeyError – if no items left |
---|---|
Returns: | object. |
See what the score for an item is.
Returns: | Number or None. |
---|
Atomically remove and return the next num items for processing in the set.
Will return at most min(num, len(self)) items. If certain items fail to deserialize, the falsey value returned will be filtered out.
Returns: | list of objects |
---|
The only builtin concrete subclasses of SortedSet are sorted sets relating to time. One class maintains order based on time (TimeSortedSet) and the other does the same, but won’t return items until their score is less than or equal to now (ScheduledSet).
Bases: redset.sets.SortedSet
A distributed, FIFO-by-default, time-sorted set that’s safe for multiprocess consumption.
Implemented in terms of a redis ZSET where UNIX timestamps are used as the score.
See redset.sets.SortedSet. Default scorer will return the current time when an item is added.
This ScheduledSet allows you to schedule items to be processed strictly in the future, which allows you to easily implement backoffs for expensive tasks that can’t be repeated continuously.
Bases: redset.sets.TimeSortedSet
A distributed, FIFO-by-default, time-sorted set that’s safe for multiprocess consumption. Supports scheduling item consumption for the future.
Implemented in terms of a redis ZSET where UNIX timestamps are used as the score.
A ScheduledSet will only return results with a score less than time.time() - to enable you to schedule jobs for the future and let redis do the work of defering them until they are ready for consumption.
See redset.sets.SortedSet. Default scorer will return the current time when an item is added.
The Serializer interface is included as a guideline for end-users. It need not be subclassed for concrete serializers.
This is a guideline for implementing a serializer for redset. Serializers need not subclass this directly, but should match the interface defined here.
Serialize a Python object into a str
Parameters: | obj – the Python object to be stored in a sorted set |
---|---|
Returns: | str |
One serializer is included for convenience, and that’s redset.serializers.NamedtupleSerializer, which allows seamless use of namedtuples.