Browse Source

Use 'app' instead of confusing 'celery' for app var name

Ask Solem 12 years ago
parent
commit
76edc99862

+ 20 - 0
celery/worker/consumer.py

@@ -387,6 +387,26 @@ class Consumer(object):
             self.strategies[name] = task.start_strategy(self.app, self)
             self.strategies[name] = task.start_strategy(self.app, self)
             task.__trace__ = build_tracer(name, task, loader, self.hostname)
             task.__trace__ = build_tracer(name, task, loader, self.hostname)
 
 
+    def create_task_handler(self, strategies, callbacks,
+            on_unknown_message, on_unknown_task, on_invalid_task):
+
+        def on_task_received(body, message):
+            if callbacks:
+                [callback() for callback in callbacks]
+            try:
+                name = body['task']
+            except (KeyError, TypeError):
+                return on_unknown_message(body, message)
+
+            try:
+                strategies[name](message, body, message.ack_log_error)
+            except KeyError as exc:
+                on_unknown_task(body, message, exc)
+            except InvalidTaskError as exc:
+                on_invalid_task(body, message, exc)
+
+        return on_task_received
+
 
 
 class Connection(bootsteps.StartStopStep):
 class Connection(bootsteps.StartStopStep):
 
 

+ 6 - 27
celery/worker/loops.py

@@ -51,20 +51,9 @@ def asynloop(obj, connection, consumer, strategies, blueprint, hub, qos,
     errors = connection.connection_errors
     errors = connection.connection_errors
     hub_add, hub_remove = hub.add, hub.remove
     hub_add, hub_remove = hub.add, hub.remove
 
 
-    def on_task_received(body, message):
-        if on_task_callbacks:
-            [callback() for callback in on_task_callbacks]
-        try:
-            name = body['task']
-        except (KeyError, TypeError):
-            return handle_unknown_message(body, message)
-
-        try:
-            strategies[name](message, body, message.ack_log_error)
-        except KeyError as exc:
-            handle_unknown_task(body, message, exc)
-        except InvalidTaskError as exc:
-            handle_invalid_task(body, message, exc)
+    on_task_received = obj.create_task_handler(
+        strategies, on_task_callbacks, handle_unknown_message,
+        handle_unknown_task, handle_invalid_task)
 
 
     if heartbeat and connection.supports_heartbeats:
     if heartbeat and connection.supports_heartbeats:
         hub.timer.apply_interval(
         hub.timer.apply_interval(
@@ -160,19 +149,9 @@ def synloop(obj, connection, consumer, strategies, blueprint, hub, qos,
             handle_invalid_task, clock, hbrate=2.0, **kwargs):
             handle_invalid_task, clock, hbrate=2.0, **kwargs):
     """Fallback blocking eventloop for transports that doesn't support AIO."""
     """Fallback blocking eventloop for transports that doesn't support AIO."""
 
 
-    def on_task_received(body, message):
-        try:
-            name = body['task']
-        except (KeyError, TypeError):
-            return handle_unknown_message(body, message)
-
-        try:
-            strategies[name](message, body, message.ack_log_error)
-        except KeyError as exc:
-            handle_unknown_task(body, message, exc)
-        except InvalidTaskError as exc:
-            handle_invalid_task(body, message, exc)
-
+    on_task_received = obj.create_task_handler(
+        strategies, [], handle_unknown_message,
+        handle_unknown_task, handle_invalid_task)
     consumer.register_callback(on_task_received)
     consumer.register_callback(on_task_received)
     consumer.consume()
     consumer.consume()
 
 

+ 9 - 7
docs/getting-started/first-steps-with-celery.rst

@@ -133,9 +133,9 @@ Let's create the file :file:`tasks.py`:
 
 
     from celery import Celery
     from celery import Celery
 
 
-    celery = Celery('tasks', broker='amqp://guest@localhost//')
+    app = Celery('tasks', broker='amqp://guest@localhost//')
 
 
-    @celery.task
+    @app.task
     def add(x, y):
     def add(x, y):
         return x + y
         return x + y
 
 
@@ -223,12 +223,12 @@ as messages.  The backend is specified via the ``backend`` argument to
 :class:`@Celery`, (or via the :setting:`CELERY_RESULT_BACKEND` setting if
 :class:`@Celery`, (or via the :setting:`CELERY_RESULT_BACKEND` setting if
 you choose to use a configuration module)::
 you choose to use a configuration module)::
 
 
-    celery = Celery('tasks', backend='amqp', broker='amqp://')
+    app = Celery('tasks', backend='amqp', broker='amqp://')
 
 
 or if you want to use Redis as the result backend, but still use RabbitMQ as
 or if you want to use Redis as the result backend, but still use RabbitMQ as
 the message broker (a popular combination)::
 the message broker (a popular combination)::
 
 
-    celery = Celery('tasks', backend='redis://localhost', broker='amqp://')
+    app = Celery('tasks', backend='redis://localhost', broker='amqp://')
 
 
 To read more about result backends please see :ref:`task-result-backends`.
 To read more about result backends please see :ref:`task-result-backends`.
 
 
@@ -288,14 +288,15 @@ task payloads by changing the :setting:`CELERY_TASK_SERIALIZER` setting:
 
 
 .. code-block:: python
 .. code-block:: python
 
 
-    celery.conf.CELERY_TASK_SERIALIZER = 'json'
+    app.conf.CELERY_TASK_SERIALIZER = 'json'
 
 
 If you are configuring many settings at once you can use ``update``:
 If you are configuring many settings at once you can use ``update``:
 
 
 .. code-block:: python
 .. code-block:: python
 
 
-    celery.conf.update(
+    app.conf.update(
         CELERY_TASK_SERIALIZER='json',
         CELERY_TASK_SERIALIZER='json',
+        CELERY_ACCEPT_CONTENT='json',  # Ignore other content
         CELERY_RESULT_SERIALIZER='json',
         CELERY_RESULT_SERIALIZER='json',
         CELERY_TIMEZONE='Europe/Oslo',
         CELERY_TIMEZONE='Europe/Oslo',
         CELERY_ENABLE_UTC=True,
         CELERY_ENABLE_UTC=True,
@@ -314,7 +315,7 @@ by calling the :meth:`~@Celery.config_from_object` method:
 
 
 .. code-block:: python
 .. code-block:: python
 
 
-    celery.config_from_object('celeryconfig')
+    app.config_from_object('celeryconfig')
 
 
 This module is often called "``celeryconfig``", but you can use any
 This module is often called "``celeryconfig``", but you can use any
 module name.
 module name.
@@ -331,6 +332,7 @@ current directory or on the Python path, it could look like this:
 
 
     CELERY_TASK_SERIALIZER = 'json'
     CELERY_TASK_SERIALIZER = 'json'
     CELERY_RESULT_SERIALIZER = 'json'
     CELERY_RESULT_SERIALIZER = 'json'
+    CELERY_ACCEPT_CONTENT='json'
     CELERY_TIMEZONE = 'Europe/Oslo'
     CELERY_TIMEZONE = 'Europe/Oslo'
     CELERY_ENABLE_UTC = True
     CELERY_ENABLE_UTC = True
 
 

+ 6 - 6
docs/getting-started/next-steps.rst

@@ -225,14 +225,14 @@ it must be in the form of ``module.path:celery``, where the part before the colo
 is the name of the module, and the attribute name comes last.
 is the name of the module, and the attribute name comes last.
 If a package name is specified instead it will automatically
 If a package name is specified instead it will automatically
 try to find a ``celery`` module in that package, and if the name
 try to find a ``celery`` module in that package, and if the name
-is a module it will try to find a ``celery`` attribute in that module.
+is a module it will try to find an app in that module.
 This means that these are all equal:
 This means that these are all equal:
 
 
 .. code-block:: bash
 .. code-block:: bash
 
 
     $ celery --app=proj
     $ celery --app=proj
     $ celery --app=proj.celery:
     $ celery --app=proj.celery:
-    $ celery --app=proj.celery:celery
+    $ celery --app=proj.celery:app
 
 
 
 
 .. _calling-tasks:
 .. _calling-tasks:
@@ -345,9 +345,9 @@ The pending state is actually not a recorded state, but rather
 the default state for any task id that is unknown, which you can see
 the default state for any task id that is unknown, which you can see
 from this example::
 from this example::
 
 
-    >>> from proj.celery import celery
+    >>> from proj.celery import app
 
 
-    >>> res = celery.AsyncResult('this-id-does-not-exist')
+    >>> res = app.AsyncResult('this-id-does-not-exist')
     >>> res.state
     >>> res.state
     'PENDING'
     'PENDING'
 
 
@@ -563,7 +563,7 @@ but it also supports simple routing where messages are sent to named queues.
 The :setting:`CELERY_ROUTES` setting enables you to route tasks by name
 The :setting:`CELERY_ROUTES` setting enables you to route tasks by name
 and keep everything centralized in one location::
 and keep everything centralized in one location::
 
 
-    celery.conf.update(
+    app.conf.update(
         CELERY_ROUTES = {
         CELERY_ROUTES = {
             'proj.tasks.add': {'queue': 'hipri'},
             'proj.tasks.add': {'queue': 'hipri'},
         },
         },
@@ -685,7 +685,7 @@ converts that UTC time to local time.  If you wish to use
 a different timezone than the system timezone then you must
 a different timezone than the system timezone then you must
 configure that using the :setting:`CELERY_TIMEZONE` setting::
 configure that using the :setting:`CELERY_TIMEZONE` setting::
 
 
-    celery.conf.CELERY_TIMEZONE = 'Europe/London'
+    app.conf.CELERY_TIMEZONE = 'Europe/London'
 
 
 Optimization
 Optimization
 ============
 ============

+ 27 - 27
docs/userguide/application.rst

@@ -20,8 +20,8 @@ Let's create one now:
 .. code-block:: python
 .. code-block:: python
 
 
     >>> from celery import Celery
     >>> from celery import Celery
-    >>> celery = Celery()
-    >>> celery
+    >>> app = Celery()
+    >>> app
     <Celery __main__:0x100469fd0>
     <Celery __main__:0x100469fd0>
 
 
 The last line shows the textual representation of the application,
 The last line shows the textual representation of the application,
@@ -45,7 +45,7 @@ Whenever you define a task, that task will also be added to the local registry:
 
 
 .. code-block:: python
 .. code-block:: python
 
 
-    >>> @celery.task
+    >>> @app.task
     ... def add(x, y):
     ... def add(x, y):
     ...     return x + y
     ...     return x + y
 
 
@@ -55,7 +55,7 @@ Whenever you define a task, that task will also be added to the local registry:
     >>> add.name
     >>> add.name
     __main__.add
     __main__.add
 
 
-    >>> celery.tasks['__main__.add']
+    >>> app.tasks['__main__.add']
     <@task: __main__.add>
     <@task: __main__.add>
 
 
 and there you see that ``__main__`` again; whenever Celery is not able
 and there you see that ``__main__`` again; whenever Celery is not able
@@ -74,16 +74,16 @@ For example here, where the tasks module is also used to start a worker:
 .. code-block:: python
 .. code-block:: python
 
 
     from celery import Celery
     from celery import Celery
-    celery = Celery()
+    app = Celery()
 
 
-    @celery.task
+    @app.task
     def add(x, y): return x + y
     def add(x, y): return x + y
 
 
     if __name__ == '__main__':
     if __name__ == '__main__':
-        celery.worker_main()
+        app.worker_main()
 
 
 When this module is executed the tasks will be named starting with "``__main__``",
 When this module is executed the tasks will be named starting with "``__main__``",
-but when it the module is imported by another process, say to call a task,
+but when the module is imported by another process, say to call a task,
 the tasks will be named starting with "``tasks``" (the real name of the module)::
 the tasks will be named starting with "``tasks``" (the real name of the module)::
 
 
     >>> from tasks import add
     >>> from tasks import add
@@ -94,11 +94,11 @@ You can specify another name for the main module:
 
 
 .. code-block:: python
 .. code-block:: python
 
 
-    >>> celery = Celery('tasks')
-    >>> celery.main
+    >>> app = Celery('tasks')
+    >>> app.main
     'tasks'
     'tasks'
 
 
-    >>> @celery.task
+    >>> @app.task
     ... def add(x, y):
     ... def add(x, y):
     ...     return x + y
     ...     return x + y
 
 
@@ -116,16 +116,16 @@ or you can use a dedicated configuration module.
 
 
 The configuration is available as :attr:`@Celery.conf`::
 The configuration is available as :attr:`@Celery.conf`::
 
 
-    >>> celery.conf.CELERY_TIMEZONE
+    >>> app.conf.CELERY_TIMEZONE
     'Europe/London'
     'Europe/London'
 
 
 where you can set configuration values directly::
 where you can set configuration values directly::
 
 
-    >>> celery.conf.CELERY_ENABLE_UTC = True
+    >>> app.conf.CELERY_ENABLE_UTC = True
 
 
 or you can update several keys at once by using the ``update`` method::
 or you can update several keys at once by using the ``update`` method::
 
 
-    >>> celery.conf.update(
+    >>> app.conf.update(
     ...     CELERY_ENABLE_UTC=True,
     ...     CELERY_ENABLE_UTC=True,
     ...     CELERY_TIMEZONE='Europe/London',
     ...     CELERY_TIMEZONE='Europe/London',
     ...)
     ...)
@@ -162,8 +162,8 @@ Example 1: Using the name of a module
 
 
     from celery import Celery
     from celery import Celery
 
 
-    celery = Celery()
-    celery.config_from_object('celeryconfig')
+    app = Celery()
+    app.config_from_object('celeryconfig')
 
 
 
 
 The ``celeryconfig`` module may then look like this:
 The ``celeryconfig`` module may then look like this:
@@ -182,9 +182,9 @@ Example 2: Using a configuration module
 
 
     from celery import Celery
     from celery import Celery
 
 
-    celery = Celery()
+    app = Celery()
     import celeryconfig
     import celeryconfig
-    celery.config_from_object(celeryconfig)
+    app.config_from_object(celeryconfig)
 
 
 Example 3:  Using a configuration class/object
 Example 3:  Using a configuration class/object
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -193,13 +193,13 @@ Example 3:  Using a configuration class/object
 
 
     from celery import Celery
     from celery import Celery
 
 
-    celery = Celery()
+    app = Celery()
 
 
     class Config:
     class Config:
         CELERY_ENABLE_UTC = True
         CELERY_ENABLE_UTC = True
         CELERY_TIMEZONE = 'Europe/London'
         CELERY_TIMEZONE = 'Europe/London'
 
 
-    celery.config_from_object(Config)
+    app.config_from_object(Config)
 
 
 ``config_from_envvar``
 ``config_from_envvar``
 ----------------------
 ----------------------
@@ -218,8 +218,8 @@ environment variable named :envvar:`CELERY_CONFIG_MODULE`:
     #: Set default configuration module name
     #: Set default configuration module name
     os.environ.setdefault('CELERY_CONFIG_MODULE', 'celeryconfig')
     os.environ.setdefault('CELERY_CONFIG_MODULE', 'celeryconfig')
 
 
-    celery = Celery()
-    celery.config_from_envvar('CELERY_CONFIG_MODULE')
+    app = Celery()
+    app.config_from_envvar('CELERY_CONFIG_MODULE')
 
 
 You can then specify the configuration module to use via the environment:
 You can then specify the configuration module to use via the environment:
 
 
@@ -251,7 +251,7 @@ you use the task, or access an attribute (in this case :meth:`repr`):
 
 
 .. code-block:: python
 .. code-block:: python
 
 
-    >>> @celery.task
+    >>> @app.task
     >>> def add(x, y):
     >>> def add(x, y):
     ...    return x + y
     ...    return x + y
 
 
@@ -422,7 +422,7 @@ You can specify a different base class with the ``base`` argument:
 
 
 .. code-block:: python
 .. code-block:: python
 
 
-    @celery.task(base=OtherTask):
+    @app.task(base=OtherTask):
     def add(x, y):
     def add(x, y):
         return x + y
         return x + y
 
 
@@ -455,14 +455,14 @@ by changing its :meth:`@Celery.Task` attribute:
 
 
     >>> from celery import Celery, Task
     >>> from celery import Celery, Task
 
 
-    >>> celery = Celery()
+    >>> app = Celery()
 
 
     >>> class MyBaseTask(Task):
     >>> class MyBaseTask(Task):
     ...    abstract = True
     ...    abstract = True
     ...    send_error_emails = True
     ...    send_error_emails = True
 
 
-    >>> celery.Task = MyBaseTask
-    >>> celery.Task
+    >>> app.Task = MyBaseTask
+    >>> app.Task
     <unbound MyBaseTask>
     <unbound MyBaseTask>
 
 
     >>> @x.task
     >>> @x.task

+ 2 - 2
docs/userguide/calling.rst

@@ -87,7 +87,7 @@ called `add`, returning the sum of two arguments:
 
 
 .. code-block:: python
 .. code-block:: python
 
 
-    @celery.task
+    @app.task
     def add(x, y):
     def add(x, y):
         return x + y
         return x + y
 
 
@@ -144,7 +144,7 @@ This is an example error callback:
 
 
 .. code-block:: python
 .. code-block:: python
 
 
-    @celery.task
+    @app.task
     def error_handler(uuid):
     def error_handler(uuid):
         result = AsyncResult(uuid)
         result = AsyncResult(uuid)
         exc = result.get(propagate=False)
         exc = result.get(propagate=False)

+ 3 - 3
docs/userguide/canvas.rst

@@ -460,11 +460,11 @@ the error callbacks take the id of the parent task as argument instead:
 
 
     from __future__ import print_function
     from __future__ import print_function
     import os
     import os
-    from proj.celery import celery
+    from proj.celery import app
 
 
-    @celery.task
+    @app.task
     def log_error(task_id):
     def log_error(task_id):
-        result = celery.AsyncResult(task_id)
+        result = app.AsyncResult(task_id)
         result.get(propagate=False)  # make sure result written.
         result.get(propagate=False)  # make sure result written.
         with open(os.path.join('/var/errors', task_id), 'a') as fh:
         with open(os.path.join('/var/errors', task_id), 'a') as fh:
             print('--\n\n{0} {1} {2}'.format(
             print('--\n\n{0} {1} {2}'.format(

+ 10 - 19
docs/userguide/monitoring.rst

@@ -514,8 +514,8 @@ Or you can use it programmatically like this:
                 recv.capture(limit=None, timeout=None)
                 recv.capture(limit=None, timeout=None)
 
 
     if __name__ == '__main__':
     if __name__ == '__main__':
-        celery = Celery(broker='amqp://guest@localhost//')
-        main(celery)
+        app = Celery(broker='amqp://guest@localhost//')
+        main(app)
 
 
 .. _event-real-time-example:
 .. _event-real-time-example:
 
 
@@ -569,8 +569,8 @@ Combining these you can easily process events in real-time:
             recv.capture(limit=None, timeout=None, wakeup=True)
             recv.capture(limit=None, timeout=None, wakeup=True)
 
 
     if __name__ == '__main__':
     if __name__ == '__main__':
-        celery = Celery(broker='amqp://guest@localhost//')
-        my_monitor(celery)
+        app = Celery(broker='amqp://guest@localhost//')
+        my_monitor(app)
 
 
 .. note::
 .. note::
 
 
@@ -590,31 +590,22 @@ You can listen to specific events by specifying the handlers:
 
 
         def announce_failed_tasks(event):
         def announce_failed_tasks(event):
             state.event(event)
             state.event(event)
-            task_id = event['uuid']
+            # task name is sent only with -received event, and state
+            # will keep track of this for us.
+            task = state.tasks.get(event['uuid'])
 
 
             print('TASK FAILED: %s[%s] %s' % (
             print('TASK FAILED: %s[%s] %s' % (
-                event['name'], task_id, state[task_id].info(), ))
-
-        def announce_dead_workers(event):
-            state.event(event)
-            hostname = event['hostname']
-
-            if not state.workers[hostname].alive:
-                print('Worker %s missed heartbeats' % (hostname, ))
-
+                task.name, task.uuid, task.info(), ))
 
 
         with app.connection() as connection:
         with app.connection() as connection:
             recv = app.events.Receiver(connection, handlers={
             recv = app.events.Receiver(connection, handlers={
                     'task-failed': announce_failed_tasks,
                     'task-failed': announce_failed_tasks,
-                    'worker-heartbeat': announce_dead_workers,
             })
             })
             recv.capture(limit=None, timeout=None, wakeup=True)
             recv.capture(limit=None, timeout=None, wakeup=True)
 
 
     if __name__ == '__main__':
     if __name__ == '__main__':
-        celery = Celery(broker='amqp://guest@localhost//')
-        my_monitor(celery)
-
-
+        app = Celery(broker='amqp://guest@localhost//')
+        my_monitor(app)
 
 
 .. _event-reference:
 .. _event-reference:
 
 

+ 33 - 30
docs/userguide/tasks.rst

@@ -51,7 +51,7 @@ the :meth:`~@Celery.task` decorator:
 
 
     from .models import User
     from .models import User
 
 
-    @celery.task
+    @app.task
     def create_user(username, password):
     def create_user(username, password):
         User.objects.create(username=username, password=password)
         User.objects.create(username=username, password=password)
 
 
@@ -61,15 +61,15 @@ these can be specified as arguments to the decorator:
 
 
 .. code-block:: python
 .. code-block:: python
 
 
-    @celery.task(serializer='json')
+    @app.task(serializer='json')
     def create_user(username, password):
     def create_user(username, password):
         User.objects.create(username=username, password=password)
         User.objects.create(username=username, password=password)
 
 
 
 
 
 
-.. sidebar:: How do I import the task decorator?
+.. sidebar:: How do I import the task decorator? And what is "app"?
 
 
-    The task decorator is available on your :class:`@Celery` instance,
+    The task decorator is available on your :class:`@Celery` application instance,
     if you don't know what that is then please read :ref:`first-steps`.
     if you don't know what that is then please read :ref:`first-steps`.
 
 
     If you're using Django or are still using the "old" module based celery API,
     If you're using Django or are still using the "old" module based celery API,
@@ -90,7 +90,7 @@ these can be specified as arguments to the decorator:
 
 
     .. code-block:: python
     .. code-block:: python
 
 
-        @celery.task
+        @app.task
         @decorator2
         @decorator2
         @decorator1
         @decorator1
         def add(x, y):
         def add(x, y):
@@ -108,7 +108,7 @@ For example:
 
 
 .. code-block:: python
 .. code-block:: python
 
 
-    >>> @celery.task(name='sum-of-two-numbers')
+    >>> @app.task(name='sum-of-two-numbers')
     >>> def add(x, y):
     >>> def add(x, y):
     ...     return x + y
     ...     return x + y
 
 
@@ -121,7 +121,7 @@ defined in another module.
 
 
 .. code-block:: python
 .. code-block:: python
 
 
-    >>> @celery.task(name='tasks.add')
+    >>> @app.task(name='tasks.add')
     >>> def add(x, y):
     >>> def add(x, y):
     ...     return x + y
     ...     return x + y
 
 
@@ -137,7 +137,7 @@ if the module name is "tasks.py":
 
 
 .. code-block:: python
 .. code-block:: python
 
 
-    @celery.task
+    @app.task
     def add(x, y):
     def add(x, y):
         return x + y
         return x + y
 
 
@@ -245,7 +245,7 @@ An example task accessing information in the context is:
 
 
 .. code-block:: python
 .. code-block:: python
 
 
-    @celery.task
+    @app.task
     def dump_context(x, y):
     def dump_context(x, y):
         print('Executing task id {0.id}, args: {0.args!r} kwargs: {0.kwargs!r}'.format(
         print('Executing task id {0.id}, args: {0.args!r} kwargs: {0.kwargs!r}'.format(
                 dump_context.request))
                 dump_context.request))
@@ -257,7 +257,7 @@ An example task accessing information in the context is:
 
 
     from celery import current_task
     from celery import current_task
 
 
-    @celery.task
+    @app.task
     def dump_context(x, y):
     def dump_context(x, y):
         print('Executing task id {0.id}, args: {0.args!r} kwargs: {0.kwargs!r}'.format(
         print('Executing task id {0.id}, args: {0.args!r} kwargs: {0.kwargs!r}'.format(
                 current_task.request))
                 current_task.request))
@@ -283,7 +283,7 @@ for all of your tasks at the top of your module:
 
 
     logger = get_task_logger(__name__)
     logger = get_task_logger(__name__)
 
 
-    @celery.task
+    @app.task
     def add(x, y):
     def add(x, y):
         logger.info('Adding {0} + {1}'.format(x, y))
         logger.info('Adding {0} + {1}'.format(x, y))
         return x + y
         return x + y
@@ -316,7 +316,7 @@ Here's an example using ``retry``:
 
 
 .. code-block:: python
 .. code-block:: python
 
 
-    @celery.task
+    @app.task
     def send_twitter_status(oauth, tweet):
     def send_twitter_status(oauth, tweet):
         try:
         try:
             twitter = Twitter(oauth)
             twitter = Twitter(oauth)
@@ -376,7 +376,7 @@ override this default.
 
 
 .. code-block:: python
 .. code-block:: python
 
 
-    @celery.task(default_retry_delay=30 * 60)  # retry in 30 minutes.
+    @app.task(default_retry_delay=30 * 60)  # retry in 30 minutes.
     def add(x, y):
     def add(x, y):
         try:
         try:
             ...
             ...
@@ -727,7 +727,7 @@ Use :meth:`~@Task.update_state` to update a task's state::
 
 
     from celery import current_task
     from celery import current_task
 
 
-    @celery.task
+    @app.task
     def upload_files(filenames):
     def upload_files(filenames):
         for i, file in enumerate(filenames):
         for i, file in enumerate(filenames):
             current_task.update_state(state='PROGRESS',
             current_task.update_state(state='PROGRESS',
@@ -809,7 +809,7 @@ As an example, the following code,
 
 
 .. code-block:: python
 .. code-block:: python
 
 
-    @celery.task
+    @app.task
     def add(x, y):
     def add(x, y):
         return x + y
         return x + y
 
 
@@ -818,7 +818,7 @@ will do roughly this behind the scenes:
 
 
 .. code-block:: python
 .. code-block:: python
 
 
-    @celery.task
+    @app.task
     class AddTask(Task):
     class AddTask(Task):
 
 
         def run(self, x, y):
         def run(self, x, y):
@@ -879,7 +879,7 @@ that can be added to tasks like this:
 .. code-block:: python
 .. code-block:: python
 
 
 
 
-    @celery.task(base=DatabaseTask)
+    @app.task(base=DatabaseTask)
     def process_rows():
     def process_rows():
         for row in process_rows.db.table.all():
         for row in process_rows.db.table.all():
             ...
             ...
@@ -904,7 +904,7 @@ base class for new task types.
             print('Task returned: {0!r}'.format(self.request)
             print('Task returned: {0!r}'.format(self.request)
 
 
 
 
-    @celery.task(base=DebugTask)
+    @app.task(base=DebugTask)
     def add(x, y):
     def add(x, y):
         return x + y
         return x + y
 
 
@@ -1038,7 +1038,7 @@ wastes time and resources.
 
 
 .. code-block:: python
 .. code-block:: python
 
 
-    @celery.task(ignore_result=True)
+    @app.task(ignore_result=True)
     def mytask(...)
     def mytask(...)
         something()
         something()
 
 
@@ -1078,21 +1078,21 @@ Make your design asynchronous instead, for example by using *callbacks*.
 
 
 .. code-block:: python
 .. code-block:: python
 
 
-    @celery.task
+    @app.task
     def update_page_info(url):
     def update_page_info(url):
         page = fetch_page.delay(url).get()
         page = fetch_page.delay(url).get()
         info = parse_page.delay(url, page).get()
         info = parse_page.delay(url, page).get()
         store_page_info.delay(url, info)
         store_page_info.delay(url, info)
 
 
-    @celery.task
+    @app.task
     def fetch_page(url):
     def fetch_page(url):
         return myhttplib.get(url)
         return myhttplib.get(url)
 
 
-    @celery.task
+    @app.task
     def parse_page(url, page):
     def parse_page(url, page):
         return myparser.parse_document(page)
         return myparser.parse_document(page)
 
 
-    @celery.task
+    @app.task
     def store_page_info(url, info):
     def store_page_info(url, info):
         return PageInfo.objects.create(url, info)
         return PageInfo.objects.create(url, info)
 
 
@@ -1106,15 +1106,15 @@ Make your design asynchronous instead, for example by using *callbacks*.
         chain = fetch_page.s() | parse_page.s() | store_page_info.s(url)
         chain = fetch_page.s() | parse_page.s() | store_page_info.s(url)
         chain()
         chain()
 
 
-    @celery.task()
+    @app.task()
     def fetch_page(url):
     def fetch_page(url):
         return myhttplib.get(url)
         return myhttplib.get(url)
 
 
-    @celery.task()
+    @app.task()
     def parse_page(page):
     def parse_page(page):
         return myparser.parse_document(page)
         return myparser.parse_document(page)
 
 
-    @celery.task(ignore_result=True)
+    @app.task(ignore_result=True)
     def store_page_info(info, url):
     def store_page_info(info, url):
         PageInfo.objects.create(url=url, info=info)
         PageInfo.objects.create(url=url, info=info)
 
 
@@ -1209,7 +1209,7 @@ that automatically expands some abbreviations in it:
         title = models.CharField()
         title = models.CharField()
         body = models.TextField()
         body = models.TextField()
 
 
-    @celery.task
+    @app.task
     def expand_abbreviations(article):
     def expand_abbreviations(article):
         article.body.replace('MyCorp', 'My Corporation')
         article.body.replace('MyCorp', 'My Corporation')
         article.save()
         article.save()
@@ -1230,7 +1230,7 @@ re-fetch the article in the task body:
 
 
 .. code-block:: python
 .. code-block:: python
 
 
-    @celery.task
+    @app.task
     def expand_abbreviations(article_id):
     def expand_abbreviations(article_id):
         article = Article.objects.get(id=article_id)
         article = Article.objects.get(id=article_id)
         article.body.replace('MyCorp', 'My Corporation')
         article.body.replace('MyCorp', 'My Corporation')
@@ -1381,7 +1381,7 @@ blog/tasks.py
 
 
 .. code-block:: python
 .. code-block:: python
 
 
-    import celery
+    from celery import Celery
 
 
     from akismet import Akismet
     from akismet import Akismet
 
 
@@ -1391,7 +1391,10 @@ blog/tasks.py
     from blog.models import Comment
     from blog.models import Comment
 
 
 
 
-    @celery.task
+    app = Celery(broker='amqp://')
+
+
+    @app.task
     def spam_filter(comment_id, remote_addr=None):
     def spam_filter(comment_id, remote_addr=None):
         logger = spam_filter.get_logger()
         logger = spam_filter.get_logger()
         logger.info('Running spam filter for comment %s', comment_id)
         logger.info('Running spam filter for comment %s', comment_id)

+ 30 - 30
docs/userguide/workers.rst

@@ -23,7 +23,7 @@ You can start the worker in the foreground by executing the command:
 
 
 .. code-block:: bash
 .. code-block:: bash
 
 
-    $ celery worker --app=app -l info
+    $ celery --app=app worker -l info
 
 
 For a full list of available command-line options see
 For a full list of available command-line options see
 :mod:`~celery.bin.worker`, or simply do:
 :mod:`~celery.bin.worker`, or simply do:
@@ -196,14 +196,14 @@ Some remote control commands also have higher-level interfaces using
 
 
 Sending the :control:`rate_limit` command and keyword arguments::
 Sending the :control:`rate_limit` command and keyword arguments::
 
 
-    >>> celery.control.broadcast('rate_limit',
+    >>> app.control.broadcast('rate_limit',
     ...                          arguments={'task_name': 'myapp.mytask',
     ...                          arguments={'task_name': 'myapp.mytask',
     ...                                     'rate_limit': '200/m'})
     ...                                     'rate_limit': '200/m'})
 
 
 This will send the command asynchronously, without waiting for a reply.
 This will send the command asynchronously, without waiting for a reply.
 To request a reply you have to use the `reply` argument::
 To request a reply you have to use the `reply` argument::
 
 
-    >>> celery.control.broadcast('rate_limit', {
+    >>> app.control.broadcast('rate_limit', {
     ...     'task_name': 'myapp.mytask', 'rate_limit': '200/m'}, reply=True)
     ...     'task_name': 'myapp.mytask', 'rate_limit': '200/m'}, reply=True)
     [{'worker1.example.com': 'New rate limit set successfully'},
     [{'worker1.example.com': 'New rate limit set successfully'},
      {'worker2.example.com': 'New rate limit set successfully'},
      {'worker2.example.com': 'New rate limit set successfully'},
@@ -212,7 +212,7 @@ To request a reply you have to use the `reply` argument::
 Using the `destination` argument you can specify a list of workers
 Using the `destination` argument you can specify a list of workers
 to receive the command::
 to receive the command::
 
 
-    >>> celery.control.broadcast('rate_limit', {
+    >>> app.control.broadcast('rate_limit', {
     ...     'task_name': 'myapp.mytask',
     ...     'task_name': 'myapp.mytask',
     ...     'rate_limit': '200/m'}, reply=True,
     ...     'rate_limit': '200/m'}, reply=True,
     ...                             destination=['worker1.example.com'])
     ...                             destination=['worker1.example.com'])
@@ -249,13 +249,13 @@ Terminating a task also revokes it.
 
 
 ::
 ::
 
 
-    >>> celery.control.revoke('d9078da5-9915-40a0-bfa1-392c7bde42ed')
+    >>> app.control.revoke('d9078da5-9915-40a0-bfa1-392c7bde42ed')
 
 
-    >>> celery.control.revoke('d9078da5-9915-40a0-bfa1-392c7bde42ed',
-    ...                       terminate=True)
+    >>> app.control.revoke('d9078da5-9915-40a0-bfa1-392c7bde42ed',
+    ...                    terminate=True)
 
 
-    >>> celery.control.revoke('d9078da5-9915-40a0-bfa1-392c7bde42ed',
-    ...                       terminate=True, signal='SIGKILL')
+    >>> app.control.revoke('d9078da5-9915-40a0-bfa1-392c7bde42ed',
+    ...                    terminate=True, signal='SIGKILL')
 
 
 .. _worker-persistent-revokes:
 .. _worker-persistent-revokes:
 
 
@@ -303,10 +303,10 @@ time limit kills it:
 
 
 .. code-block:: python
 .. code-block:: python
 
 
-    from myapp import celery
+    from myapp import app
     from celery.exceptions import SoftTimeLimitExceeded
     from celery.exceptions import SoftTimeLimitExceeded
 
 
-    @celery.task
+    @app.task
     def mytask():
     def mytask():
         try:
         try:
             do_work()
             do_work()
@@ -335,8 +335,8 @@ Example changing the time limit for the ``tasks.crawl_the_web`` task
 to have a soft time limit of one minute, and a hard time limit of
 to have a soft time limit of one minute, and a hard time limit of
 two minutes::
 two minutes::
 
 
-    >>> celery.control.time_limit('tasks.crawl_the_web',
-                                  soft=60, hard=120, reply=True)
+    >>> app.control.time_limit('tasks.crawl_the_web',
+                               soft=60, hard=120, reply=True)
     [{'worker1.example.com': {'ok': 'time limits set successfully'}}]
     [{'worker1.example.com': {'ok': 'time limits set successfully'}}]
 
 
 Only tasks that starts executing after the time limit change will be affected.
 Only tasks that starts executing after the time limit change will be affected.
@@ -354,12 +354,12 @@ Changing rate-limits at runtime
 Example changing the rate limit for the `myapp.mytask` task to accept
 Example changing the rate limit for the `myapp.mytask` task to accept
 200 tasks a minute on all servers::
 200 tasks a minute on all servers::
 
 
-    >>> celery.control.rate_limit('myapp.mytask', '200/m')
+    >>> app.control.rate_limit('myapp.mytask', '200/m')
 
 
 Example changing the rate limit on a single host by specifying the
 Example changing the rate limit on a single host by specifying the
 destination host name::
 destination host name::
 
 
-    >>> celery.control.rate_limit('myapp.mytask', '200/m',
+    >>> app.control.rate_limit('myapp.mytask', '200/m',
     ...            destination=['worker1.example.com'])
     ...            destination=['worker1.example.com'])
 
 
 .. warning::
 .. warning::
@@ -633,23 +633,23 @@ being imported by the worker processes:
 
 
 .. code-block:: python
 .. code-block:: python
 
 
-    >>> celery.control.broadcast('pool_restart',
-    ...                          arguments={'modules': ['foo', 'bar']})
+    >>> app.control.broadcast('pool_restart',
+    ...                       arguments={'modules': ['foo', 'bar']})
 
 
 Use the ``reload`` argument to reload modules it has already imported:
 Use the ``reload`` argument to reload modules it has already imported:
 
 
 .. code-block:: python
 .. code-block:: python
 
 
-    >>> celery.control.broadcast('pool_restart',
-    ...                          arguments={'modules': ['foo'],
-    ...                                     'reload': True})
+    >>> app.control.broadcast('pool_restart',
+    ...                       arguments={'modules': ['foo'],
+    ...                                  'reload': True})
 
 
 If you don't specify any modules then all known tasks modules will
 If you don't specify any modules then all known tasks modules will
 be imported/reloaded:
 be imported/reloaded:
 
 
 .. code-block:: python
 .. code-block:: python
 
 
-    >>> celery.control.broadcast('pool_restart', arguments={'reload': True})
+    >>> app.control.broadcast('pool_restart', arguments={'reload': True})
 
 
 The ``modules`` argument is a list of modules to modify. ``reload``
 The ``modules`` argument is a list of modules to modify. ``reload``
 specifies whether to reload modules if they have previously been imported.
 specifies whether to reload modules if they have previously been imported.
@@ -684,14 +684,14 @@ and it supports the same commands as the :class:`@Celery.control` interface.
 .. code-block:: python
 .. code-block:: python
 
 
     # Inspect all nodes.
     # Inspect all nodes.
-    >>> i = celery.control.inspect()
+    >>> i = app.control.inspect()
 
 
     # Specify multiple nodes to inspect.
     # Specify multiple nodes to inspect.
-    >>> i = celery.control.inspect(['worker1.example.com',
-                                    'worker2.example.com'])
+    >>> i = app.control.inspect(['worker1.example.com',
+                                'worker2.example.com'])
 
 
     # Specify a single node to inspect.
     # Specify a single node to inspect.
-    >>> i = celery.control.inspect('worker1.example.com')
+    >>> i = app.control.inspect('worker1.example.com')
 
 
 .. _worker-inspect-registered-tasks:
 .. _worker-inspect-registered-tasks:
 
 
@@ -776,8 +776,8 @@ Remote shutdown
 
 
 This command will gracefully shut down the worker remotely::
 This command will gracefully shut down the worker remotely::
 
 
-    >>> celery.control.broadcast('shutdown') # shutdown all workers
-    >>> celery.control.broadcast('shutdown, destination='worker1.example.com')
+    >>> app.control.broadcast('shutdown') # shutdown all workers
+    >>> app.control.broadcast('shutdown, destination='worker1.example.com')
 
 
 .. control:: ping
 .. control:: ping
 
 
@@ -789,7 +789,7 @@ The workers reply with the string 'pong', and that's just about it.
 It will use the default one second timeout for replies unless you specify
 It will use the default one second timeout for replies unless you specify
 a custom timeout::
 a custom timeout::
 
 
-    >>> celery.control.ping(timeout=0.5)
+    >>> app.control.ping(timeout=0.5)
     [{'worker1.example.com': 'pong'},
     [{'worker1.example.com': 'pong'},
      {'worker2.example.com': 'pong'},
      {'worker2.example.com': 'pong'},
      {'worker3.example.com': 'pong'}]
      {'worker3.example.com': 'pong'}]
@@ -815,8 +815,8 @@ a worker using :program:`celery events`/:program:`celerymon`.
 
 
 .. code-block:: python
 .. code-block:: python
 
 
-    >>> celery.control.enable_events()
-    >>> celery.control.disable_events()
+    >>> app.control.enable_events()
+    >>> app.control.disable_events()
 
 
 .. _worker-custom-control-commands:
 .. _worker-custom-control-commands:
 
 

+ 2 - 2
docs/whatsnew-3.0.rst

@@ -673,14 +673,14 @@ The :option:`--app` option now 'auto-detects'
       and get the celery attribute from that module.
       and get the celery attribute from that module.
 
 
 E.g. if you have a project named 'proj' where the
 E.g. if you have a project named 'proj' where the
-celery app is located in 'from proj.celery import celery',
+celery app is located in 'from proj.celery import app',
 then the following will be equivalent:
 then the following will be equivalent:
 
 
 .. code-block:: bash
 .. code-block:: bash
 
 
         $ celery worker --app=proj
         $ celery worker --app=proj
         $ celery worker --app=proj.celery:
         $ celery worker --app=proj.celery:
-        $ celery worker --app=proj.celery:celery
+        $ celery worker --app=proj.celery:app
 
 
 In Other News
 In Other News
 -------------
 -------------

+ 6 - 6
examples/next-steps/proj/celery.py

@@ -2,15 +2,15 @@ from __future__ import absolute_import
 
 
 from celery import Celery
 from celery import Celery
 
 
-celery = Celery('proj.celery',
-                broker='amqp://',
-                backend='amqp://',
-                include=['proj.tasks'])
+app = Celery('proj.celery',
+             broker='amqp://',
+             backend='amqp://',
+             include=['proj.tasks'])
 
 
 # Optional configuration, see the application user guide.
 # Optional configuration, see the application user guide.
-celery.conf.update(
+app.conf.update(
     CELERY_TASK_RESULT_EXPIRES=3600,
     CELERY_TASK_RESULT_EXPIRES=3600,
 )
 )
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':
-    celery.start()
+    app.start()

+ 4 - 4
examples/next-steps/proj/tasks.py

@@ -1,18 +1,18 @@
 from __future__ import absolute_import
 from __future__ import absolute_import
 
 
-from proj.celery import celery
+from proj.celery import app
 
 
 
 
-@celery.task
+@app.task
 def add(x, y):
 def add(x, y):
     return x + y
     return x + y
 
 
 
 
-@celery.task
+@app.task
 def mul(x, y):
 def mul(x, y):
     return x * y
     return x * y
 
 
 
 
-@celery.task
+@app.task
 def xsum(numbers):
 def xsum(numbers):
     return sum(numbers)
     return sum(numbers)