This document describes the current stable version of Celery (4.0). For development docs, go here.

celery.utils.collections

Custom maps, sets, sequences, and other data structures.

class celery.utils.collections.AttributeDictMixin[source]

Mixin for Mapping interface that adds attribute access.

I.e., d.key -> d[key]).

class celery.utils.collections.AttributeDict[source]

Dict subclass with attribute access.

class celery.utils.collections.BufferMap(maxsize, iterable=None, bufmaxsize=1000)[source]

Map of buffers.

Buffer

alias of Messagebuffer

exception Empty

Exception raised by Queue.get(block=0)/get_nowait().

BufferMap.bufmaxsize = None
BufferMap.extend(key, it)[source]
BufferMap.maxsize = None
BufferMap.put(key, item)[source]
BufferMap.take(key, *default)[source]
BufferMap.total = 0
class celery.utils.collections.ChainMap(*maps, **kwargs)[source]

Key lookup on a sequence of maps.

add_defaults(d)[source]
changes = None
clear()[source]
copy()[source]
defaults = None
classmethod fromkeys(iterable, *args)[source]

Create a ChainMap with a single dict created from the iterable.

get(key, default=None)[source]
items()[source]
iteritems()
iterkeys()
itervalues()
key_t = None
keys()[source]
maps = None
pop(key, *default)[source]
setdefault(key, default=None)[source]
update(*args, **kwargs)[source]
values()[source]
class celery.utils.collections.ConfigurationView(changes, defaults=None, keys=None, prefix=None)[source]

A view over an applications configuration dictionaries.

Custom (but older) version of collections.ChainMap.

If the key does not exist in changes, the defaults dictionaries are consulted.

Parameters:
  • changes (Mapping) – Map of configuration changes.
  • defaults (List[Mapping]) – List of dictionaries containing the default configuration.
clear()[source]

Remove all changes, but keep defaults.

first(*keys)[source]
get(key, default=None)[source]
swap_with(other)[source]
class celery.utils.collections.DictAttribute(obj)[source]

Dict interface to attributes.

obj[k] -> obj.k obj[k] = val -> obj.k = val

get(key, default=None)[source]
items()[source]
iteritems()
iterkeys()
itervalues()
keys()[source]
obj = None
setdefault(key, default=None)[source]
values()[source]
class celery.utils.collections.Evictable[source]

Mixin for classes supporting the evict method.

exception Empty

Exception raised by Queue.get(block=0)/get_nowait().

Evictable.evict()[source]

Force evict until maxsize is enforced.

class celery.utils.collections.LimitedSet(maxlen=0, expires=0, data=None, minlen=0)[source]

Kind-of Set (or priority queue) with limitations.

Good for when you need to test for membership (a in set), but the set should not grow unbounded.

maxlen is enforced at all times, so if the limit is reached we’ll also remove non-expired items.

You can also configure minlen: this is the minimal residual size of the set.

All arguments are optional, and no limits are enabled by default.

Parameters:
  • maxlen (int) – Optional max number of items. Adding more items than maxlen will result in immediate removal of items sorted by oldest insertion time.
  • expires (float) – TTL for all items. Expired items are purged as keys are inserted.
  • minlen (int) –

    Minimal residual size of this set. .. versionadded:: 4.0

    Value must be less than maxlen if both are configured.

    Older expired items will be deleted, only after the set exceeds minlen number of items.

  • data (Sequence) – Initial data to initialize set with. Can be an iterable of (key, value) pairs, a dict ({key: insertion_time}), or another instance of LimitedSet.

Example

>>> s = LimitedSet(maxlen=50000, expires=3600, minlen=4000)
>>> for i in range(60000):
...     s.add(i)
...     s.add(str(i))
...
>>> 57000 in s  # last 50k inserted values are kept
True
>>> '10' in s  # '10' did expire and was purged from set.
False
>>> len(s)  # maxlen is reached
50000
>>> s.purge(now=time.time() + 7200)  # clock + 2 hours
>>> len(s)  # now only minlen items are cached
4000
>>>> 57000 in s  # even this item is gone now
False
add(item, now=None)[source]

Add a new item, or reset the expiry time of an existing item.

as_dict()[source]

Whole set as serializable dictionary.

Example

>>> s = LimitedSet(maxlen=200)
>>> r = LimitedSet(maxlen=200)
>>> for i in range(500):
...     s.add(i)
...
>>> r.update(s.as_dict())
>>> r == s
True
clear()[source]

Clear all data, start from scratch again.

discard(item)[source]
max_heap_percent_overload = 15
pop(default=None)[source]

Remove and return the oldest item, or None when empty.

pop_value(item)
purge(now=None)[source]

Check oldest items and remove them if needed.

Parameters:now (float) – Time of purging – by default right now. This can be useful for unit testing.
update(other)[source]

Update this set from other LimitedSet, dict or iterable.

class celery.utils.collections.Messagebuffer(maxsize, iterable=None, deque=<type 'collections.deque'>)[source]

A buffer of pending messages.

exception Empty

Exception raised by Queue.get(block=0)/get_nowait().

Messagebuffer.extend(it)[source]
Messagebuffer.put(item)[source]
Messagebuffer.take(*default)[source]
class celery.utils.collections.OrderedDict(*args, **kwds)[source]

Dict where insertion order matters.

move_to_end(key, last=True)[source]
celery.utils.collections.force_mapping(m)[source]

Wrap object into supporting the mapping interface if necessary.

celery.utils.collections.lpmerge(L, R)[source]

In place left precedent dictionary merge.

Keeps values from L, if the value in R is None.