app.base

Actual App instance implementation.

Module Contents

Classes

PendingConfiguration(self,conf,callback)
Celery(self,main=None,loader=None,backend=None,amqp=None,events=None,log=None,control=None,set_as_current=True,tasks=None,broker=None,include=None,changes=None,config_source=None,fixups=None,task_cls=None,autofinalize=True,namespace=None,strict_typing=True,**kwargs) Celery application.

Functions

app_has_custom(app,attr) Return true if app has customized method attr.
_unpickle_appattr(reverse_name,args) Unpickle app.
_after_fork_cleanup_app(app)
app_has_custom(app, attr)

Return true if app has customized method attr.

Note:
This is used for optimizations in cases where we know how the default behavior works, but need to account for someone using inheritance to override a method/property.
_unpickle_appattr(reverse_name, args)

Unpickle app.

_after_fork_cleanup_app(app)
class PendingConfiguration(conf, callback)
__init__(conf, callback)
__setitem__(key, value)
clear()
update(*args, **kwargs)
setdefault(*args, **kwargs)
__contains__(key)
__len__()
__repr__()
data()
class Celery(main=None, loader=None, backend=None, amqp=None, events=None, log=None, control=None, set_as_current=True, tasks=None, broker=None, include=None, changes=None, config_source=None, fixups=None, task_cls=None, autofinalize=True, namespace=None, strict_typing=True, **kwargs)

Celery application.

Arguments:
main (str): Name of the main module if running as __main__.
This is used as the prefix for auto-generated task names.
Keyword Arguments:

broker (str): URL of the default broker used. backend (Union[str, type]): The result store backend class,

or the name of the backend class to use.

Default is the value of the :setting:`result_backend` setting.

autofinalize (bool): If set to False a RuntimeError
will be raised if the task registry or tasks are used before the app is finalized.

set_as_current (bool): Make this the global current app. include (List[str]): List of modules every worker should import.

amqp (Union[str, type]): AMQP object or class name. events (Union[str, type]): Events object or class name. log (Union[str, type]): Log object or class name. control (Union[str, type]): Control object or class name. tasks (Union[str, type]): A task registry, or the name of

a registry class.
fixups (List[str]): List of fix-up plug-ins (e.g., see
celery.fixups.django).
config_source (Union[str, type]): Take configuration from a class,
or object. Attributes may include any settings described in the documentation.
__init__(main=None, loader=None, backend=None, amqp=None, events=None, log=None, control=None, set_as_current=True, tasks=None, broker=None, include=None, changes=None, config_source=None, fixups=None, task_cls=None, autofinalize=True, namespace=None, strict_typing=True, **kwargs)
_get_default_loader()
on_init()

Optional callback called at init.

__autoset(key, value)
set_current()

Make this the current app for this thread.

set_default()

Make this the default app for all threads.

_ensure_after_fork()
close()

Clean up after the application.

Only necessary for dynamically created apps, and you should probably use the with statement instead.

Example:
>>> with Celery(set_as_current=False) as app:
...     with app.connection_for_write() as conn:
...         pass
start(argv=None)

Run celery using argv.

Uses sys.argv if argv is not specified.

worker_main(argv=None)

Run celery worker using argv.

Uses sys.argv if argv is not specified.

task(*args, **opts)

Decorator to create a task class out of any callable.

Examples:
@app.task
def refresh_feed(url):
    store_feed(feedparser.parse(url))

with setting extra options:

@app.task(exchange='feeds')
def refresh_feed(url):
    return store_feed(feedparser.parse(url))
Note:

App Binding: For custom apps the task decorator will return a proxy object, so that the act of creating the task is not performed until the task is used or the task registry is accessed.

If you’re depending on binding to be deferred, then you must not access any attributes on the returned object until the application is fully set up (finalized).

_task_from_fun(fun, name=None, base=None, bind=False, **options)
register_task(task)

Utility for registering a task-based class.

Note:
This is here for compatibility with old Celery 1.0 style task classes, you should not need to use this for new projects.
gen_task_name(name, module)
finalize(auto=False)

Finalize the app.

This loads built-in tasks, evaluates pending task decorators, reads configuration, etc.

add_defaults(fun)

Add default configuration from dict d.

If the argument is a callable function then it will be regarded as a promise, and it won’t be loaded until the configuration is actually needed.

This method can be compared to:

>>> celery.conf.update(d)

with a difference that 1) no copy will be made and 2) the dict will not be transferred when the worker spawns child processes, so it’s important that the same configuration happens at import time when pickle restores the object on the other side.

config_from_object(obj, silent=False, force=False, namespace=None)

Read configuration from object.

Object is either an actual object or the name of a module to import.

Example:
>>> celery.config_from_object('myapp.celeryconfig')
>>> from myapp import celeryconfig
>>> celery.config_from_object(celeryconfig)
Arguments:

silent (bool): If true then import errors will be ignored. force (bool): Force reading configuration immediately.

By default the configuration will be read only when required.
config_from_envvar(variable_name, silent=False, force=False)

Read configuration from environment variable.

The value of the environment variable must be the name of a module to import.

Example:
>>> os.environ['CELERY_CONFIG_MODULE'] = 'myapp.celeryconfig'
>>> celery.config_from_envvar('CELERY_CONFIG_MODULE')
config_from_cmdline(argv, namespace="celery")
setup_security(allowed_serializers=None, key=None, cert=None, store=None, digest="sha1", serializer="json")

Setup the message-signing serializer.

This will affect all application instances (a global operation).

Disables untrusted serializers and if configured to use the auth serializer will register the auth serializer with the provided settings into the Kombu serializer registry.

Arguments:
allowed_serializers (Set[str]): List of serializer names, or
content_types that should be exempt from being disabled.
key (str): Name of private key file to use.
Defaults to the :setting:`security_key` setting.
cert (str): Name of certificate file to use.
Defaults to the :setting:`security_certificate` setting.
store (str): Directory containing certificates.
Defaults to the :setting:`security_cert_store` setting.
digest (str): Digest algorithm used when signing messages.
Default is sha1.
serializer (str): Serializer used to encode messages after
they’ve been signed. See :setting:`task_serializer` for the serializers supported. Default is json.
autodiscover_tasks(packages=None, related_name="tasks", force=False)

Auto-discover task modules.

Searches a list of packages for a “tasks.py” module (or use related_name argument).

If the name is empty, this will be delegated to fix-ups (e.g., Django).

For example if you have a directory layout like this:

foo/__init__.py
   tasks.py
   models.py

bar/__init__.py
    tasks.py
    models.py

baz/__init__.py
    models.py

Then calling app.autodiscover_tasks(['foo', 'bar', 'baz']) will result in the modules foo.tasks and bar.tasks being imported.

Arguments:
packages (List[str]): List of packages to search.
This argument may also be a callable, in which case the value returned is used (for lazy evaluation).
related_name (str): The name of the module to find. Defaults
to “tasks”: meaning “look for ‘module.tasks’ for every module in packages.”
force (bool): By default this call is lazy so that the actual
auto-discovery won’t happen until an application imports the default modules. Forcing will cause the auto-discovery to happen immediately.
_autodiscover_tasks(packages, related_name, **kwargs)
_autodiscover_tasks_from_names(packages, related_name)
_autodiscover_tasks_from_fixups(related_name)
send_task(name, args=None, kwargs=None, countdown=None, eta=None, task_id=None, producer=None, connection=None, router=None, result_cls=None, expires=None, publisher=None, link=None, link_error=None, add_to_parent=True, group_id=None, retries=0, chord=None, reply_to=None, time_limit=None, soft_time_limit=None, root_id=None, parent_id=None, route_name=None, shadow=None, chain=None, task_type=None, **options)

Send task by name.

Supports the same arguments as @-Task.apply_async().

Arguments:
name (str): Name of task to call (e.g., “tasks.add”). result_cls (AsyncResult): Specify custom result class.
connection_for_read(url=None, **kwargs)

Establish connection used for consuming.

See Also:
connection() for supported arguments.
connection_for_write(url=None, **kwargs)

Establish connection used for producing.

See Also:
connection() for supported arguments.
connection(hostname=None, userid=None, password=None, virtual_host=None, port=None, ssl=None, connect_timeout=None, transport=None, transport_options=None, heartbeat=None, login_method=None, failover_strategy=None, **kwargs)

Establish a connection to the message broker.

Please use connection_for_read() and connection_for_write() instead, to convey the intent of use for this connection.

Arguments:

url: Either the URL or the hostname of the broker to use. hostname (str): URL, Hostname/IP-address of the broker.

If a URL is used, then the other argument below will be taken from the URL instead.

userid (str): Username to authenticate as. password (str): Password to authenticate with virtual_host (str): Virtual host to use (domain). port (int): Port to connect to. ssl (bool, Dict): Defaults to the :setting:`broker_use_ssl`

setting.
transport (str): defaults to the :setting:`broker_transport`
setting.

transport_options (Dict): Dictionary of transport specific options. heartbeat (int): AMQP Heartbeat in seconds (pyamqp only). login_method (str): Custom login method to use (AMQP only). failover_strategy (str, Callable): Custom failover strategy. **kwargs: Additional arguments to kombu.Connection.

Returns:
kombu.Connection: the lazy connection instance.
_connection(url, userid=None, password=None, virtual_host=None, port=None, ssl=None, connect_timeout=None, transport=None, transport_options=None, heartbeat=None, login_method=None, failover_strategy=None, **kwargs)
_acquire_connection(pool=True)

Helper for connection_or_acquire().

connection_or_acquire(connection=None, pool=True, *_, **__)

Context used to acquire a connection from the pool.

For use within a with statement to get a connection from the pool if one is not already provided.

Arguments:
connection (kombu.Connection): If not provided, a connection
will be acquired from the connection pool.
producer_or_acquire(producer=None)

Context used to acquire a producer from the pool.

For use within a with statement to get a producer from the pool if one is not already provided

Arguments:
producer (kombu.Producer): If not provided, a producer
will be acquired from the producer pool.
prepare_config(c)

Prepare configuration before it is merged with the defaults.

now()

Return the current time and date as a datetime.

select_queues(queues=None)

Select subset of queues.

Arguments:
queues (Sequence[str]): a list of queue names to keep.
either(default_key, *defaults)

Get key from configuration or use default values.

Fallback to the value of a configuration key if none of the *values are true.

bugreport()

Return information useful in bug reports.

_get_backend()
_finalize_pending_conf()

Get config value by key and finalize loading the configuration.

Note:
This is used by PendingConfiguration:
as soon as you access a key the configuration is read.
_load_config()
_after_fork()
signature(*args, **kwargs)

Return a new Signature bound to this app.

add_periodic_task(schedule, sig, args=tuple, kwargs=tuple, name=None, **opts)
_sig_to_periodic_task_entry(schedule, sig, args=tuple, kwargs=dict, name=None, **opts)
_add_periodic_task(key, entry)
create_task_cls()

Create a base task class bound to this app.

subclass_with_self(Class, name=None, attribute="app", reverse=None, keep_reduce=False, **kw)

Subclass an app-compatible class.

App-compatible means that the class has a class attribute that provides the default app it should use, for example: class Foo: app = None.

Arguments:

Class (type): The app-compatible class to subclass. name (str): Custom name for the target class. attribute (str): Name of the attribute holding the app,

Default is ‘app’.
reverse (str): Reverse path to this object used for pickling
purposes. For example, to get app.AsyncResult, use "AsyncResult".
keep_reduce (bool): If enabled a custom __reduce__
implementation won’t be provided.
_rgetattr(path)
__enter__()
__exit__(*exc_info)
__repr__()
__reduce__()
__reduce_v1__()
__reduce_keys__()

Keyword arguments used to reconstruct the object when unpickling.

__reduce_args__()

Deprecated method, please use __reduce_keys__() instead.

Worker()

Worker application.

See Also:
@Worker.
WorkController(**kwargs)

Embeddable worker.

See Also:
@WorkController.
Beat(**kwargs)

celery beat scheduler application.

See Also:
@Beat.
Task()

Base task class for this app.

annotations()
AsyncResult()

Create new result instance.

See Also:
celery.result.AsyncResult.
ResultSet()
GroupResult()

Create new group result instance.

See Also:
celery.result.GroupResult.
pool()

Broker connection pool: @pool.

Note:
This attribute is not related to the workers concurrency pool.
current_task()

Instance of task being executed, or None.

current_worker_task()

The task currently being executed by a worker or None.

Differs from current_task in that it’s not affected by tasks calling other tasks directly, or eagerly.

oid()

Universally unique identifier for this app.

amqp()

AMQP related functionality: @amqp.

backend()

Current backend instance.

conf()

Current configuration.

conf(d)
control()

Remote control: @control.

events()

Consuming and sending events: @events.

loader()

Current loader instance.

log()

Logging: @log.

_canvas()
tasks()

Task registry.

Warning:
Accessing this attribute will also auto-finalize the app.
producer_pool()
uses_utc_timezone()

Check if the application uses the UTC timezone.

timezone()

Current timezone for this app.

This is a cached property taking the time zone from the :setting:`timezone` setting.