This document describes an older version of Celery (2.2). For the latest stable version please go here.

celery.app

Celery Application.

copyright:
  1. 2009 - 2011 by Ask Solem.
license:

BSD, see LICENSE for more details.

Application

class celery.app.App(main=None, loader=None, backend=None, amqp=None, events=None, log=None, control=None, set_as_current=True, accept_magic_kwargs=False)

Celery Application.

Parameters:
  • main – Name of the main module if running as __main__.
  • loader – The loader class, or the name of the loader class to use. Default is celery.loaders.app.AppLoader.
  • backend – The result store backend class, or the name of the backend class to use. Default is the value of the CELERY_RESULT_BACKEND setting.
  • amqp – AMQP object or class name.
  • events – Events object or class name.
  • log – Log object or class name.
  • control – Control object or class name.
  • set_as_current – Make this the global current app.
main

Name of the __main__ module. Required for standalone scripts.

If set this will be used instead of __main__ when automatically generating task names.

amqp

Sending/receiving messages. See AMQP.

backend

Storing/retreiving task state. See BaseBackend.

loader

Current loader.

conf

Current configuration (dict and attribute access).

control

Controlling worker nodes. See Control.

log

Logging utilities. See Logging.

config_from_object(obj, silent=False)

Read configuration from object, where object is either a real object, or the name of an object to import.

>>> celery.config_from_object("myapp.celeryconfig")
>>> from myapp import celeryconfig
>>> celery.config_from_object(celeryconfig)
config_from_envvar(variable_name, silent=False)

Read configuration from environment variable.

The value of the environment variable must be the name of an object to import.

>>> os.environ["CELERY_CONFIG_MODULE"] = "myapp.celeryconfig"
>>> celery.config_from_envvar("CELERY_CONFIG_MODULE")
config_from_cmdline(argv, namespace='celery')

Read configuration from argv.

The config

task(*args, **options)

Decorator to create a task class out of any callable.

Examples

@task()
def refresh_feed(url):
    return Feed.objects.get(url=url).refresh()

With setting extra options and using retry.

@task(exchange="feeds")
def refresh_feed(url, **kwargs):
    try:
        return Feed.objects.get(url=url).refresh()
    except socket.error, exc:
        refresh_feed.retry(args=[url], kwargs=kwargs, exc=exc)

Calling the resulting task:

>>> refresh_feed("http://example.com/rss") # Regular
<Feed: http://example.com/rss>
>>> refresh_feed.delay("http://example.com/rss") # Async
<AsyncResult: 8998d0f4-da0b-4669-ba03-d5ab5ac6ad5d>
create_task_cls()

Creates a base task class using default configuration taken from this app.

TaskSet(*args, **kwargs)

Create new TaskSet.

send_task(name, args=None, kwargs=None, countdown=None, eta=None, task_id=None, publisher=None, connection=None, connect_timeout=None, result_cls=None, expires=None, queues=None, **options)

Send task by name.

Parameters:
  • name – Name of task to execute (e.g. “tasks.add”).
  • result_cls – Specify custom result class. Default is using AsyncResult().

Supports the same arguments as apply_async().

AsyncResult(task_id, backend=None, task_name=None)

Create celery.result.BaseAsyncResult instance.

TaskSetResult(taskset_id, results, **kwargs)

Create celery.result.TaskSetResult instance.

worker_main(argv=None)

Run celeryd using argv. Uses sys.argv if argv is not specified.

Worker(**kwargs)

Create new Worker instance.

Beat(**kwargs)

Create new Beat instance.

broker_connection(hostname=None, userid=None, password=None, virtual_host=None, port=None, ssl=None, insist=None, connect_timeout=None, transport=None, **kwargs)

Establish a connection to the message broker.

Parameters:

:returns kombu.connection.BrokerConnection:

with_default_connection(fun)

With any function accepting connection and connect_timeout keyword arguments, establishes a default connection if one is not already passed to it.

Any automatically established connection will be closed after the function returns.

mail_admins(subject, body, fail_silently=False)

Send an e-mail to the admins in the ADMINS setting.

prepare_config(c)

Prepare configuration before it is merged with the defaults.

either(default_key, *values)

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

merge(l, r)

Like dict(a, **b) except it will keep values from a if the value in b is None.

Functions

celery.app.app_or_default(app=None)

Returns the app provided or the default app if none.

The environment variable CELERY_TRACE_APP is used to trace app leaks. When enabled an exception is raised if there is no active app.

Previous topic

API Reference

Next topic

celery.app.amqp

This Page