| 1 |
============================= |
| 2 |
"The Big Instance" Refactor |
| 3 |
============================= |
| 4 |
|
| 5 |
The `app` branch is a work-in-progress to remove |
| 6 |
the use of a global configuration in Celery. |
| 7 |
|
| 8 |
Celery can now be instantiated and several |
| 9 |
instances of Celery may exist in the same process space. |
| 10 |
Also, large parts can be customized without resorting to monkey |
| 11 |
patching. |
| 12 |
|
| 13 |
Examples |
| 14 |
======== |
| 15 |
|
| 16 |
Creating a Celery instance:: |
| 17 |
|
| 18 |
>>> from celery import Celery |
| 19 |
>>> app = Celery() |
| 20 |
>>> app.config_from_object('celeryconfig') |
| 21 |
>>> #app.config_from_envvar('CELERY_CONFIG_MODULE') |
| 22 |
|
| 23 |
|
| 24 |
Creating tasks: |
| 25 |
|
| 26 |
.. code-block:: python |
| 27 |
|
| 28 |
@app.task |
| 29 |
def add(x, y): |
| 30 |
return x + y |
| 31 |
|
| 32 |
|
| 33 |
Creating custom Task subclasses: |
| 34 |
|
| 35 |
.. code-block:: python |
| 36 |
|
| 37 |
Task = celery.create_task_cls() |
| 38 |
|
| 39 |
class DebugTask(Task): |
| 40 |
|
| 41 |
def on_failure(self, *args, **kwargs): |
| 42 |
import pdb |
| 43 |
pdb.set_trace() |
| 44 |
|
| 45 |
@app.task(base=DebugTask) |
| 46 |
def add(x, y): |
| 47 |
return x + y |
| 48 |
|
| 49 |
Starting a worker: |
| 50 |
|
| 51 |
.. code-block:: python |
| 52 |
|
| 53 |
worker = celery.Worker(loglevel='INFO') |
| 54 |
|
| 55 |
Getting access to the configuration: |
| 56 |
|
| 57 |
.. code-block:: python |
| 58 |
|
| 59 |
celery.conf.task_always_eager = True |
| 60 |
celery.conf['task_always_eager'] = True |
| 61 |
|
| 62 |
|
| 63 |
Controlling workers:: |
| 64 |
|
| 65 |
>>> celery.control.inspect().active() |
| 66 |
>>> celery.control.rate_limit(add.name, '100/m') |
| 67 |
>>> celery.control.broadcast('shutdown') |
| 68 |
>>> celery.control.discard_all() |
| 69 |
|
| 70 |
Other interesting attributes:: |
| 71 |
|
| 72 |
# Establish broker connection. |
| 73 |
>>> celery.broker_connection() |
| 74 |
|
| 75 |
# AMQP Specific features. |
| 76 |
>>> celery.amqp |
| 77 |
>>> celery.amqp.Router |
| 78 |
>>> celery.amqp.get_queues() |
| 79 |
>>> celery.amqp.get_task_consumer() |
| 80 |
|
| 81 |
# Loader |
| 82 |
>>> celery.loader |
| 83 |
|
| 84 |
# Default backend |
| 85 |
>>> celery.backend |
| 86 |
|
| 87 |
|
| 88 |
As you can probably see, this really opens up another |
| 89 |
dimension of customization abilities. |
| 90 |
|
| 91 |
Deprecated |
| 92 |
========== |
| 93 |
|
| 94 |
* ``celery.task.ping`` |
| 95 |
``celery.task.PingTask`` |
| 96 |
|
| 97 |
Inferior to the ping remote control command. |
| 98 |
Will be removed in Celery 2.3. |
| 99 |
|
| 100 |
Aliases (Pending deprecation) |
| 101 |
============================= |
| 102 |
|
| 103 |
* ``celery.execute`` |
| 104 |
* ``.send_task`` -> {``app.send_task``} |
| 105 |
* ``.delay_task`` -> *no alternative* |
| 106 |
|
| 107 |
* ``celery.log`` |
| 108 |
* ``.get_default_logger`` -> {``app.log.get_default_logger``} |
| 109 |
* ``.setup_logger`` -> {``app.log.setup_logger``} |
| 110 |
* ``.get_task_logger`` -> {``app.log.get_task_logger``} |
| 111 |
* ``.setup_task_logger`` -> {``app.log.setup_task_logger``} |
| 112 |
* ``.setup_logging_subsystem`` -> {``app.log.setup_logging_subsystem``} |
| 113 |
* ``.redirect_stdouts_to_logger`` -> {``app.log.redirect_stdouts_to_logger``} |
| 114 |
|
| 115 |
* ``celery.messaging`` |
| 116 |
* ``.establish_connection`` -> {``app.broker_connection``} |
| 117 |
* ``.with_connection`` -> {``app.with_connection``} |
| 118 |
* ``.get_consumer_set`` -> {``app.amqp.get_task_consumer``} |
| 119 |
* ``.TaskPublisher`` -> {``app.amqp.TaskPublisher``} |
| 120 |
* ``.TaskConsumer`` -> {``app.amqp.TaskConsumer``} |
| 121 |
* ``.ConsumerSet`` -> {``app.amqp.ConsumerSet``} |
| 122 |
|
| 123 |
* ``celery.conf.*`` -> {``app.conf``} |
| 124 |
|
| 125 |
**NOTE**: All configuration keys are now named the same |
| 126 |
as in the configuration. So the key ``task_always_eager`` |
| 127 |
is accessed as:: |
| 128 |
|
| 129 |
>>> app.conf.task_always_eager |
| 130 |
|
| 131 |
instead of:: |
| 132 |
|
| 133 |
>>> from celery import conf |
| 134 |
>>> conf.always_eager |
| 135 |
|
| 136 |
* ``.get_queues`` -> {``app.amqp.get_queues``} |
| 137 |
|
| 138 |
* ``celery.utils.info`` |
| 139 |
* ``.humanize_seconds`` -> ``celery.utils.time.humanize_seconds`` |
| 140 |
* ``.textindent`` -> ``celery.utils.textindent`` |
| 141 |
* ``.get_broker_info`` -> {``app.amqp.get_broker_info``} |
| 142 |
* ``.format_broker_info`` -> {``app.amqp.format_broker_info``} |
| 143 |
* ``.format_queues`` -> {``app.amqp.format_queues``} |
| 144 |
|
| 145 |
Default App Usage |
| 146 |
================= |
| 147 |
|
| 148 |
To be backward compatible, it must be possible |
| 149 |
to use all the classes/functions without passing |
| 150 |
an explicit app instance. |
| 151 |
|
| 152 |
This is achieved by having all app-dependent objects |
| 153 |
use :data:`~celery.app.default_app` if the app instance |
| 154 |
is missing. |
| 155 |
|
| 156 |
.. code-block:: python |
| 157 |
|
| 158 |
from celery.app import app_or_default |
| 159 |
|
| 160 |
class SomeClass: |
| 161 |
|
| 162 |
def __init__(self, app=None): |
| 163 |
self.app = app_or_default(app) |
| 164 |
|
| 165 |
The problem with this approach is that there's a chance |
| 166 |
that the app instance is lost along the way, and everything |
| 167 |
seems to be working normally. Testing app instance leaks |
| 168 |
is hard. The environment variable :envvar:`CELERY_TRACE_APP` |
| 169 |
can be used, when this is enabled :func:`celery.app.app_or_default` |
| 170 |
will raise an exception whenever it has to go back to the default app |
| 171 |
instance. |
| 172 |
|
| 173 |
App Dependency Tree |
| 174 |
------------------- |
| 175 |
|
| 176 |
* {``app``} |
| 177 |
* ``celery.loaders.base.BaseLoader`` |
| 178 |
* ``celery.backends.base.BaseBackend`` |
| 179 |
* {``app.TaskSet``} |
| 180 |
* ``celery.task.sets.TaskSet`` (``app.TaskSet``) |
| 181 |
* [``app.TaskSetResult``] |
| 182 |
* ``celery.result.TaskSetResult`` (``app.TaskSetResult``) |
| 183 |
|
| 184 |
* {``app.AsyncResult``} |
| 185 |
* ``celery.result.BaseAsyncResult`` / ``celery.result.AsyncResult`` |
| 186 |
|
| 187 |
* ``celery.bin.worker.WorkerCommand`` |
| 188 |
* ``celery.apps.worker.Worker`` |
| 189 |
* ``celery.worker.WorkerController`` |
| 190 |
* ``celery.worker.consumer.Consumer`` |
| 191 |
* ``celery.worker.request.Request`` |
| 192 |
* ``celery.events.EventDispatcher`` |
| 193 |
* ``celery.worker.control.ControlDispatch`` |
| 194 |
* ``celery.worker.control.registry.Panel`` |
| 195 |
* ``celery.pidbox.BroadcastPublisher`` |
| 196 |
* ``celery.pidbox.BroadcastConsumer`` |
| 197 |
* ``celery.beat.EmbeddedService`` |
| 198 |
|
| 199 |
* ``celery.bin.events.EvCommand`` |
| 200 |
* ``celery.events.snapshot.evcam`` |
| 201 |
* ``celery.events.snapshot.Polaroid`` |
| 202 |
* ``celery.events.EventReceiver`` |
| 203 |
* ``celery.events.cursesmon.evtop`` |
| 204 |
* ``celery.events.EventReceiver`` |
| 205 |
* ``celery.events.cursesmon.CursesMonitor`` |
| 206 |
* ``celery.events.dumper`` |
| 207 |
* ``celery.events.EventReceiver`` |
| 208 |
|
| 209 |
* ``celery.bin.amqp.AMQPAdmin`` |
| 210 |
|
| 211 |
* ``celery.bin.beat.BeatCommand`` |
| 212 |
* ``celery.apps.beat.Beat`` |
| 213 |
* ``celery.beat.Service`` |
| 214 |
* ``celery.beat.Scheduler`` |