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

celery.utils

Utility functions.

Don’t import from here directly anymore, as these are only here for backwards compatibility.

class celery.utils.cached_property(fget=None, fset=None, fdel=None, doc=None)[source]

Cached property descriptor.

Caches the return value of the get method on first call.

Examples

@cached_property
def connection(self):
    return Connection()

@connection.setter  # Prepares stored value
def connection(self, value):
    if value is None:
        raise TypeError('Connection must be a connection')
    return value

@connection.deleter
def connection(self, value):
    # Additional action to do at del(self.attr)
    if value is not None:
        print('Connection {0!r} deleted'.format(value)
deleter(fdel)[source]
setter(fset)[source]
celery.utils.chunks(it, n)[source]

Split an iterator into chunks with n elements each.

Warning

it must be an actual iterator, if you pass this a concrete sequence will get you repeating elements.

So chunks(iter(range(1000)), 10) is fine, but chunks(range(1000), 10) is not.

Example

# n == 2 >>> x = chunks(iter([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), 2) >>> list(x) [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9], [10]]

# n == 3 >>> x = chunks(iter([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), 3) >>> list(x) [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10]]

celery.utils.gen_task_name(app, name, module_name)[source]

Generate task name from name/module pair.

celery.utils.gen_unique_id(_uuid=<function uuid4>)

Generate unique id in UUID4 format.

See also

For now this is provided by uuid.uuid4().

celery.utils.get_cls_by_name(name, aliases=None, imp=None, package=None, sep='.', default=None, **kwargs)

Get symbol by qualified name.

The name should be the full dot-separated path to the class:

modulename.ClassName

Example:

celery.concurrency.processes.TaskPool
                            ^- class name

or using ‘:’ to separate module and symbol:

celery.concurrency.processes:TaskPool

If aliases is provided, a dict containing short name/long name mappings, the name is looked up in the aliases first.

Examples

>>> symbol_by_name('celery.concurrency.processes.TaskPool')
<class 'celery.concurrency.processes.TaskPool'>
>>> symbol_by_name('default', {
...     'default': 'celery.concurrency.processes.TaskPool'})
<class 'celery.concurrency.processes.TaskPool'>

# Does not try to look up non-string names. >>> from celery.concurrency.processes import TaskPool >>> symbol_by_name(TaskPool) is TaskPool True

celery.utils.get_full_cls_name(obj)

Return object name.

celery.utils.import_from_cwd(module, imp=None, package=None)[source]

Import module, temporarily including modules in the current directory.

Modules located in the current directory has precedence over modules located in sys.path.

celery.utils.instantiate(name, *args, **kwargs)[source]

Instantiate class by name.

See also

symbol_by_name().

celery.utils.memoize(maxsize=None, keyfun=None, Cache=<class 'kombu.utils.functional.LRUCache'>)[source]

Decorator to cache function return value.

celery.utils.nodename(name, hostname)[source]

Create node name from name/hostname pair.

celery.utils.nodesplit(name)[source]

Split node name into tuple of name/hostname.

celery.utils.noop(*args, **kwargs)[source]

No operation.

Takes any arguments/keyword arguments and does nothing.

celery.utils.uuid(_uuid=<function uuid4>)[source]

Generate unique id in UUID4 format.

See also

For now this is provided by uuid.uuid4().

celery.utils.worker_direct(hostname)[source]

Return the kombu.Queue being a direct route to a worker.

Parameters

hostname (str, Queue) – The fully qualified node name of a worker (e.g., w1@example.com). If passed a kombu.Queue instance it will simply return that instead.