| 1 |
.. _internals-guide: |
| 2 |
|
| 3 |
================================ |
| 4 |
Contributors Guide to the Code |
| 5 |
================================ |
| 6 |
|
| 7 |
.. contents:: |
| 8 |
:local: |
| 9 |
|
| 10 |
Philosophy |
| 11 |
========== |
| 12 |
|
| 13 |
The API>RCP Precedence Rule |
| 14 |
--------------------------- |
| 15 |
|
| 16 |
- The API is more important than Readability |
| 17 |
- Readability is more important than Convention |
| 18 |
- Convention is more important than Performance |
| 19 |
- …unless the code is a proven hot-spot. |
| 20 |
|
| 21 |
More important than anything else is the end-user API. |
| 22 |
Conventions must step aside, and any suffering is always alleviated |
| 23 |
if the end result is a better API. |
| 24 |
|
| 25 |
Conventions and Idioms Used |
| 26 |
=========================== |
| 27 |
|
| 28 |
Classes |
| 29 |
------- |
| 30 |
|
| 31 |
Naming |
| 32 |
~~~~~~ |
| 33 |
|
| 34 |
- Follows :pep:`8`. |
| 35 |
|
| 36 |
- Class names must be `CamelCase`. |
| 37 |
- but not if they're verbs, verbs shall be `lower_case`: |
| 38 |
|
| 39 |
.. code-block:: python |
| 40 |
|
| 41 |
# - test case for a class |
| 42 |
class TestMyClass(Case): # BAD |
| 43 |
pass |
| 44 |
|
| 45 |
class test_MyClass(Case): # GOOD |
| 46 |
pass |
| 47 |
|
| 48 |
# - test case for a function |
| 49 |
class TestMyFunction(Case): # BAD |
| 50 |
pass |
| 51 |
|
| 52 |
class test_my_function(Case): # GOOD |
| 53 |
pass |
| 54 |
|
| 55 |
# - "action" class (verb) |
| 56 |
class UpdateTwitterStatus: # BAD |
| 57 |
pass |
| 58 |
|
| 59 |
class update_twitter_status: # GOOD |
| 60 |
pass |
| 61 |
|
| 62 |
.. note:: |
| 63 |
|
| 64 |
Sometimes it makes sense to have a class mask as a function, |
| 65 |
and there's precedence for this in the Python standard library (e.g., |
| 66 |
:class:`~contextlib.contextmanager`). Celery examples include |
| 67 |
:class:`~celery.signature`, :class:`~celery.chord`, |
| 68 |
``inspect``, :class:`~kombu.utils.functional.promise` and more.. |
| 69 |
|
| 70 |
- Factory functions and methods must be `CamelCase` (excluding verbs): |
| 71 |
|
| 72 |
.. code-block:: python |
| 73 |
|
| 74 |
class Celery: |
| 75 |
|
| 76 |
def consumer_factory(self): # BAD |
| 77 |
... |
| 78 |
|
| 79 |
def Consumer(self): # GOOD |
| 80 |
... |
| 81 |
|
| 82 |
Default values |
| 83 |
~~~~~~~~~~~~~~ |
| 84 |
|
| 85 |
Class attributes serve as default values for the instance, |
| 86 |
as this means that they can be set by either instantiation or inheritance. |
| 87 |
|
| 88 |
**Example:** |
| 89 |
|
| 90 |
.. code-block:: python |
| 91 |
|
| 92 |
class Producer: |
| 93 |
active = True |
| 94 |
serializer = 'json' |
| 95 |
|
| 96 |
def __init__(self, serializer=None, active=None): |
| 97 |
self.serializer = serializer or self.serializer |
| 98 |
|
| 99 |
# must check for None when value can be false-y |
| 100 |
self.active = active if active is not None else self.active |
| 101 |
|
| 102 |
A subclass can change the default value: |
| 103 |
|
| 104 |
.. code-block:: python |
| 105 |
|
| 106 |
TaskProducer(Producer): |
| 107 |
serializer = 'pickle' |
| 108 |
|
| 109 |
and the value can be set at instantiation: |
| 110 |
|
| 111 |
.. code-block:: pycon |
| 112 |
|
| 113 |
>>> producer = TaskProducer(serializer='msgpack') |
| 114 |
|
| 115 |
Exceptions |
| 116 |
~~~~~~~~~~ |
| 117 |
|
| 118 |
Custom exceptions raised by an objects methods and properties |
| 119 |
should be available as an attribute and documented in the |
| 120 |
method/property that throw. |
| 121 |
|
| 122 |
This way a user doesn't have to find out where to import the |
| 123 |
exception from, but rather use ``help(obj)`` and access |
| 124 |
the exception class from the instance directly. |
| 125 |
|
| 126 |
**Example**: |
| 127 |
|
| 128 |
.. code-block:: python |
| 129 |
|
| 130 |
class Empty(Exception): |
| 131 |
pass |
| 132 |
|
| 133 |
class Queue: |
| 134 |
Empty = Empty |
| 135 |
|
| 136 |
def get(self): |
| 137 |
"""Get the next item from the queue. |
| 138 |
|
| 139 |
:raises Queue.Empty: if there are no more items left. |
| 140 |
|
| 141 |
""" |
| 142 |
try: |
| 143 |
return self.queue.popleft() |
| 144 |
except IndexError: |
| 145 |
raise self.Empty() |
| 146 |
|
| 147 |
Composites |
| 148 |
~~~~~~~~~~ |
| 149 |
|
| 150 |
Similarly to exceptions, composite classes should be override-able by |
| 151 |
inheritance and/or instantiation. Common sense can be used when |
| 152 |
selecting what classes to include, but often it's better to add one |
| 153 |
too many: predicting what users need to override is hard (this has |
| 154 |
saved us from many a monkey patch). |
| 155 |
|
| 156 |
**Example**: |
| 157 |
|
| 158 |
.. code-block:: python |
| 159 |
|
| 160 |
class Worker: |
| 161 |
Consumer = Consumer |
| 162 |
|
| 163 |
def __init__(self, connection, consumer_cls=None): |
| 164 |
self.Consumer = consumer_cls or self.Consumer |
| 165 |
|
| 166 |
def do_work(self): |
| 167 |
with self.Consumer(self.connection) as consumer: |
| 168 |
self.connection.drain_events() |
| 169 |
|
| 170 |
Applications vs. "single mode" |
| 171 |
============================== |
| 172 |
|
| 173 |
In the beginning Celery was developed for Django, simply because |
| 174 |
this enabled us get the project started quickly, while also having |
| 175 |
a large potential user base. |
| 176 |
|
| 177 |
In Django there's a global settings object, so multiple Django projects |
| 178 |
can't co-exist in the same process space, this later posed a problem |
| 179 |
for using Celery with frameworks that don't have this limitation. |
| 180 |
|
| 181 |
Therefore the app concept was introduced. When using apps you use 'celery' |
| 182 |
objects instead of importing things from Celery sub-modules, this |
| 183 |
(unfortunately) also means that Celery essentially has two API's. |
| 184 |
|
| 185 |
Here's an example using Celery in single-mode: |
| 186 |
|
| 187 |
.. code-block:: python |
| 188 |
|
| 189 |
from celery import task |
| 190 |
from celery.task.control import inspect |
| 191 |
|
| 192 |
from .models import CeleryStats |
| 193 |
|
| 194 |
@task |
| 195 |
def write_stats_to_db(): |
| 196 |
stats = inspect().stats(timeout=1) |
| 197 |
for node_name, reply in stats: |
| 198 |
CeleryStats.objects.update_stat(node_name, stats) |
| 199 |
|
| 200 |
|
| 201 |
and here's the same using Celery app objects: |
| 202 |
|
| 203 |
.. code-block:: python |
| 204 |
|
| 205 |
from .celery import celery |
| 206 |
from .models import CeleryStats |
| 207 |
|
| 208 |
@app.task |
| 209 |
def write_stats_to_db(): |
| 210 |
stats = celery.control.inspect().stats(timeout=1) |
| 211 |
for node_name, reply in stats: |
| 212 |
CeleryStats.objects.update_stat(node_name, stats) |
| 213 |
|
| 214 |
|
| 215 |
In the example above the actual application instance is imported |
| 216 |
from a module in the project, this module could look something like this: |
| 217 |
|
| 218 |
.. code-block:: python |
| 219 |
|
| 220 |
from celery import Celery |
| 221 |
|
| 222 |
app = Celery(broker='amqp://') |
| 223 |
|
| 224 |
|
| 225 |
Module Overview |
| 226 |
=============== |
| 227 |
|
| 228 |
- celery.app |
| 229 |
|
| 230 |
This is the core of Celery: the entry-point for all functionality. |
| 231 |
|
| 232 |
- celery.loaders |
| 233 |
|
| 234 |
Every app must have a loader. The loader decides how configuration |
| 235 |
is read; what happens when the worker starts; when a task starts and ends; |
| 236 |
and so on. |
| 237 |
|
| 238 |
The loaders included are: |
| 239 |
|
| 240 |
- app |
| 241 |
|
| 242 |
Custom Celery app instances uses this loader by default. |
| 243 |
|
| 244 |
- default |
| 245 |
|
| 246 |
"single-mode" uses this loader by default. |
| 247 |
|
| 248 |
Extension loaders also exist, for example :pypi:`celery-pylons`. |
| 249 |
|
| 250 |
- celery.worker |
| 251 |
|
| 252 |
This is the worker implementation. |
| 253 |
|
| 254 |
- celery.backends |
| 255 |
|
| 256 |
Task result backends live here. |
| 257 |
|
| 258 |
- celery.apps |
| 259 |
|
| 260 |
Major user applications: worker and beat. |
| 261 |
The command-line wrappers for these are in celery.bin (see below) |
| 262 |
|
| 263 |
- celery.bin |
| 264 |
|
| 265 |
Command-line applications. |
| 266 |
:file:`setup.py` creates setuptools entry-points for these. |
| 267 |
|
| 268 |
- celery.concurrency |
| 269 |
|
| 270 |
Execution pool implementations (prefork, eventlet, gevent, solo, thread). |
| 271 |
|
| 272 |
- celery.db |
| 273 |
|
| 274 |
Database models for the SQLAlchemy database result backend. |
| 275 |
(should be moved into :mod:`celery.backends.database`) |
| 276 |
|
| 277 |
- celery.events |
| 278 |
|
| 279 |
Sending and consuming monitoring events, also includes curses monitor, |
| 280 |
event dumper and utilities to work with in-memory cluster state. |
| 281 |
|
| 282 |
- celery.execute.trace |
| 283 |
|
| 284 |
How tasks are executed and traced by the worker, and in eager mode. |
| 285 |
|
| 286 |
- celery.security |
| 287 |
|
| 288 |
Security related functionality, currently a serializer using |
| 289 |
cryptographic digests. |
| 290 |
|
| 291 |
- celery.task |
| 292 |
|
| 293 |
single-mode interface to creating tasks, and controlling workers. |
| 294 |
|
| 295 |
- t.unit (int distribution) |
| 296 |
|
| 297 |
The unit test suite. |
| 298 |
|
| 299 |
- celery.utils |
| 300 |
|
| 301 |
Utility functions used by the Celery code base. |
| 302 |
Much of it is there to be compatible across Python versions. |
| 303 |
|
| 304 |
- celery.contrib |
| 305 |
|
| 306 |
Additional public code that doesn't fit into any other name-space. |
| 307 |
|
| 308 |
Worker overview |
| 309 |
=============== |
| 310 |
|
| 311 |
* `celery.bin.worker:Worker` |
| 312 |
|
| 313 |
This is the command-line interface to the worker. |
| 314 |
|
| 315 |
Responsibilities: |
| 316 |
* Daemonization when :option:`--detach <celery worker --detach>` set, |
| 317 |
* dropping privileges when using :option:`--uid <celery worker --uid>`/ |
| 318 |
:option:`--gid <celery worker --gid>` arguments |
| 319 |
* Installs "concurrency patches" (eventlet/gevent monkey patches). |
| 320 |
|
| 321 |
``app.worker_main(argv)`` calls |
| 322 |
``instantiate('celery.bin.worker:Worker')(app).execute_from_commandline(argv)`` |
| 323 |
|
| 324 |
* `app.Worker` -> `celery.apps.worker:Worker` |
| 325 |
|
| 326 |
Responsibilities: |
| 327 |
* sets up logging and redirects standard outs |
| 328 |
* installs signal handlers (`TERM`/`HUP`/`STOP`/`USR1` (cry)/`USR2` (rdb)) |
| 329 |
* prints banner and warnings (e.g., pickle warning) |
| 330 |
* handles the :option:`celery worker --purge` argument |
| 331 |
|
| 332 |
* `app.WorkController` -> `celery.worker.WorkController` |
| 333 |
|
| 334 |
This is the real worker, built up around bootsteps. |