| 1 |
.. _intro: |
| 2 |
|
| 3 |
======================== |
| 4 |
Introduction to Celery |
| 5 |
======================== |
| 6 |
|
| 7 |
.. contents:: |
| 8 |
:local: |
| 9 |
:depth: 1 |
| 10 |
|
| 11 |
What's a Task Queue? |
| 12 |
==================== |
| 13 |
|
| 14 |
Task queues are used as a mechanism to distribute work across threads or |
| 15 |
machines. |
| 16 |
|
| 17 |
A task queue's input is a unit of work called a task. Dedicated worker |
| 18 |
processes constantly monitor task queues for new work to perform. |
| 19 |
|
| 20 |
Celery communicates via messages, usually using a broker |
| 21 |
to mediate between clients and workers. To initiate a task the client adds a |
| 22 |
message to the queue, the broker then delivers that message to a worker. |
| 23 |
|
| 24 |
A Celery system can consist of multiple workers and brokers, giving way |
| 25 |
to high availability and horizontal scaling. |
| 26 |
|
| 27 |
Celery is written in Python, but the protocol can be implemented in any |
| 28 |
language. In addition to Python there's node-celery_ for Node.js, |
| 29 |
a `PHP client`_, `gocelery`_, `gopher-celery`_ for Go, and `rusty-celery`_ for Rust. |
| 30 |
|
| 31 |
Language interoperability can also be achieved |
| 32 |
exposing an HTTP endpoint and having a task that requests it (webhooks). |
| 33 |
|
| 34 |
.. _node-celery: https://github.com/mher/node-celery |
| 35 |
.. _`PHP client`: https://github.com/gjedeer/celery-php |
| 36 |
.. _`gocelery`: https://github.com/gocelery/gocelery |
| 37 |
.. _`gopher-celery`: https://github.com/marselester/gopher-celery |
| 38 |
.. _`rusty-celery`: https://github.com/rusty-celery/rusty-celery |
| 39 |
|
| 40 |
What do I need? |
| 41 |
=============== |
| 42 |
|
| 43 |
.. sidebar:: Version Requirements |
| 44 |
:subtitle: Celery version 5.5.x runs on: |
| 45 |
|
| 46 |
- Python ❨3.8, 3.9, 3.10, 3.11, 3.12, 3.13❩ |
| 47 |
- PyPy3.9+ ❨v7.3.12+❩ |
| 48 |
|
| 49 |
If you're running an older version of Python, you need to be running |
| 50 |
an older version of Celery: |
| 51 |
|
| 52 |
- Python 3.7: Celery 5.2 or earlier. |
| 53 |
- Python 3.6: Celery 5.1 or earlier. |
| 54 |
- Python 2.7: Celery 4.x series. |
| 55 |
- Python 2.6: Celery series 3.1 or earlier. |
| 56 |
- Python 2.5: Celery series 3.0 or earlier. |
| 57 |
- Python 2.4: Celery series 2.2 or earlier.. |
| 58 |
|
| 59 |
Celery is a project with minimal funding, |
| 60 |
so we don't support Microsoft Windows. |
| 61 |
Please don't open any issues related to that platform. |
| 62 |
|
| 63 |
*Celery* requires a message transport to send and receive messages. |
| 64 |
The RabbitMQ and Redis broker transports are feature complete, |
| 65 |
but there's also support for a myriad of other experimental solutions, including |
| 66 |
using SQLite for local development. |
| 67 |
|
| 68 |
*Celery* can run on a single machine, on multiple machines, or even |
| 69 |
across data centers. |
| 70 |
|
| 71 |
Get Started |
| 72 |
=========== |
| 73 |
|
| 74 |
If this is the first time you're trying to use Celery, or if you haven't |
| 75 |
kept up with development in the 3.1 version and are coming from previous versions, |
| 76 |
then you should read our getting started tutorials: |
| 77 |
|
| 78 |
- :ref:`first-steps` |
| 79 |
- :ref:`next-steps` |
| 80 |
|
| 81 |
Celery is… |
| 82 |
========== |
| 83 |
|
| 84 |
.. _`mailing-list`: https://groups.google.com/group/celery-users |
| 85 |
|
| 86 |
.. topic:: \ |
| 87 |
|
| 88 |
- **Simple** |
| 89 |
|
| 90 |
Celery is easy to use and maintain, and it *doesn't need configuration files*. |
| 91 |
|
| 92 |
It has an active, friendly community you can talk to for support, |
| 93 |
including a `mailing-list`_ and an :ref:`IRC channel <irc-channel>`. |
| 94 |
|
| 95 |
Here's one of the simplest applications you can make: |
| 96 |
|
| 97 |
.. code-block:: python |
| 98 |
|
| 99 |
from celery import Celery |
| 100 |
|
| 101 |
app = Celery('hello', broker='amqp://guest@localhost//') |
| 102 |
|
| 103 |
@app.task |
| 104 |
def hello(): |
| 105 |
return 'hello world' |
| 106 |
|
| 107 |
- **Highly Available** |
| 108 |
|
| 109 |
Workers and clients will automatically retry in the event |
| 110 |
of connection loss or failure, and some brokers support |
| 111 |
HA in way of *Primary/Primary* or *Primary/Replica* replication. |
| 112 |
|
| 113 |
- **Fast** |
| 114 |
|
| 115 |
A single Celery process can process millions of tasks a minute, |
| 116 |
with sub-millisecond round-trip latency (using RabbitMQ, |
| 117 |
librabbitmq, and optimized settings). |
| 118 |
|
| 119 |
- **Flexible** |
| 120 |
|
| 121 |
Almost every part of *Celery* can be extended or used on its own, |
| 122 |
Custom pool implementations, serializers, compression schemes, logging, |
| 123 |
schedulers, consumers, producers, broker transports, and much more. |
| 124 |
|
| 125 |
|
| 126 |
.. topic:: It supports |
| 127 |
|
| 128 |
.. hlist:: |
| 129 |
:columns: 2 |
| 130 |
|
| 131 |
- **Brokers** |
| 132 |
|
| 133 |
- :ref:`RabbitMQ <broker-rabbitmq>`, :ref:`Redis <broker-redis>`, |
| 134 |
- :ref:`Amazon SQS <broker-sqs>`, and more… |
| 135 |
|
| 136 |
- **Concurrency** |
| 137 |
|
| 138 |
- prefork (multiprocessing), |
| 139 |
- Eventlet_, gevent_ |
| 140 |
- thread (multithreaded) |
| 141 |
- `solo` (single threaded) |
| 142 |
|
| 143 |
- **Result Stores** |
| 144 |
|
| 145 |
- AMQP, Redis |
| 146 |
- Memcached, |
| 147 |
- SQLAlchemy, Django ORM |
| 148 |
- Apache Cassandra, Elasticsearch, Riak |
| 149 |
- MongoDB, CouchDB, Couchbase, ArangoDB |
| 150 |
- Amazon DynamoDB, Amazon S3 |
| 151 |
- Microsoft Azure Block Blob, Microsoft Azure Cosmos DB |
| 152 |
- Google Cloud Storage |
| 153 |
- File system |
| 154 |
|
| 155 |
- **Serialization** |
| 156 |
|
| 157 |
- *pickle*, *json*, *yaml*, *msgpack*. |
| 158 |
- *zlib*, *bzip2* compression. |
| 159 |
- Cryptographic message signing. |
| 160 |
|
| 161 |
Features |
| 162 |
======== |
| 163 |
|
| 164 |
.. topic:: \ |
| 165 |
|
| 166 |
.. hlist:: |
| 167 |
:columns: 2 |
| 168 |
|
| 169 |
- **Monitoring** |
| 170 |
|
| 171 |
A stream of monitoring events is emitted by workers and |
| 172 |
is used by built-in and external tools to tell you what |
| 173 |
your cluster is doing -- in real-time. |
| 174 |
|
| 175 |
:ref:`Read more… <guide-monitoring>`. |
| 176 |
|
| 177 |
- **Work-flows** |
| 178 |
|
| 179 |
Simple and complex work-flows can be composed using |
| 180 |
a set of powerful primitives we call the "canvas", |
| 181 |
including grouping, chaining, chunking, and more. |
| 182 |
|
| 183 |
:ref:`Read more… <guide-canvas>`. |
| 184 |
|
| 185 |
- **Time & Rate Limits** |
| 186 |
|
| 187 |
You can control how many tasks can be executed per second/minute/hour, |
| 188 |
or how long a task can be allowed to run, and this can be set as |
| 189 |
a default, for a specific worker or individually for each task type. |
| 190 |
|
| 191 |
:ref:`Read more… <worker-time-limits>`. |
| 192 |
|
| 193 |
- **Scheduling** |
| 194 |
|
| 195 |
You can specify the time to run a task in seconds or a |
| 196 |
:class:`~datetime.datetime`, or you can use |
| 197 |
periodic tasks for recurring events based on a |
| 198 |
simple interval, or Crontab expressions |
| 199 |
supporting minute, hour, day of week, day of month, and |
| 200 |
month of year. |
| 201 |
|
| 202 |
:ref:`Read more… <guide-beat>`. |
| 203 |
|
| 204 |
- **Resource Leak Protection** |
| 205 |
|
| 206 |
The :option:`--max-tasks-per-child <celery worker --max-tasks-per-child>` |
| 207 |
option is used for user tasks leaking resources, like memory or |
| 208 |
file descriptors, that are simply out of your control. |
| 209 |
|
| 210 |
:ref:`Read more… <worker-max-tasks-per-child>`. |
| 211 |
|
| 212 |
- **User Components** |
| 213 |
|
| 214 |
Each worker component can be customized, and additional components |
| 215 |
can be defined by the user. The worker is built up using "bootsteps" — a |
| 216 |
dependency graph enabling fine grained control of the worker's |
| 217 |
internals. |
| 218 |
|
| 219 |
.. _`Eventlet`: http://eventlet.net/ |
| 220 |
.. _`gevent`: http://gevent.org/ |
| 221 |
|
| 222 |
Framework Integration |
| 223 |
===================== |
| 224 |
|
| 225 |
Celery is easy to integrate with web frameworks, some of them even have |
| 226 |
integration packages: |
| 227 |
|
| 228 |
+--------------------+------------------------+ |
| 229 |
| `Pyramid`_ | :pypi:`pyramid_celery` | |
| 230 |
+--------------------+------------------------+ |
| 231 |
| `Pylons`_ | :pypi:`celery-pylons` | |
| 232 |
+--------------------+------------------------+ |
| 233 |
| `Flask`_ | not needed | |
| 234 |
+--------------------+------------------------+ |
| 235 |
| `web2py`_ | :pypi:`web2py-celery` | |
| 236 |
+--------------------+------------------------+ |
| 237 |
| `Tornado`_ | :pypi:`tornado-celery` | |
| 238 |
+--------------------+------------------------+ |
| 239 |
| `Tryton`_ | :pypi:`celery_tryton` | |
| 240 |
+--------------------+------------------------+ |
| 241 |
|
| 242 |
For `Django`_ see :ref:`django-first-steps`. |
| 243 |
|
| 244 |
The integration packages aren't strictly necessary, but they can make |
| 245 |
development easier, and sometimes they add important hooks like closing |
| 246 |
database connections at :manpage:`fork(2)`. |
| 247 |
|
| 248 |
.. _`Django`: https://djangoproject.com/ |
| 249 |
.. _`Pylons`: http://pylonshq.com/ |
| 250 |
.. _`Flask`: http://flask.pocoo.org/ |
| 251 |
.. _`web2py`: http://web2py.com/ |
| 252 |
.. _`Bottle`: https://bottlepy.org/ |
| 253 |
.. _`Pyramid`: http://docs.pylonsproject.org/en/latest/docs/pyramid.html |
| 254 |
.. _`Tornado`: http://www.tornadoweb.org/ |
| 255 |
.. _`Tryton`: http://www.tryton.org/ |
| 256 |
.. _`tornado-celery`: https://github.com/mher/tornado-celery/ |
| 257 |
|
| 258 |
Quick Jump |
| 259 |
========== |
| 260 |
|
| 261 |
.. topic:: I want to ⟶ |
| 262 |
|
| 263 |
.. hlist:: |
| 264 |
:columns: 2 |
| 265 |
|
| 266 |
- :ref:`get the return value of a task <task-states>` |
| 267 |
- :ref:`use logging from my task <task-logging>` |
| 268 |
- :ref:`learn about best practices <task-best-practices>` |
| 269 |
- :ref:`create a custom task base class <task-custom-classes>` |
| 270 |
- :ref:`add a callback to a group of tasks <canvas-chord>` |
| 271 |
- :ref:`split a task into several chunks <canvas-chunks>` |
| 272 |
- :ref:`optimize the worker <guide-optimizing>` |
| 273 |
- :ref:`see a list of built-in task states <task-builtin-states>` |
| 274 |
- :ref:`create custom task states <custom-states>` |
| 275 |
- :ref:`set a custom task name <task-names>` |
| 276 |
- :ref:`track when a task starts <task-track-started>` |
| 277 |
- :ref:`retry a task when it fails <task-retry>` |
| 278 |
- :ref:`get the id of the current task <task-request-info>` |
| 279 |
- :ref:`know what queue a task was delivered to <task-request-info>` |
| 280 |
- :ref:`see a list of running workers <monitoring-control>` |
| 281 |
- :ref:`purge all messages <monitoring-control>` |
| 282 |
- :ref:`inspect what the workers are doing <monitoring-control>` |
| 283 |
- :ref:`see what tasks a worker has registered <monitoring-control>` |
| 284 |
- :ref:`migrate tasks to a new broker <monitoring-control>` |
| 285 |
- :ref:`see a list of event message types <event-reference>` |
| 286 |
- :ref:`contribute to Celery <contributing>` |
| 287 |
- :ref:`learn about available configuration settings <configuration>` |
| 288 |
- :ref:`get a list of people and companies using Celery <res-using-celery>` |
| 289 |
- :ref:`write my own remote control command <worker-custom-control-commands>` |
| 290 |
- :ref:`change worker queues at runtime <worker-queues>` |
| 291 |
|
| 292 |
.. topic:: Jump to ⟶ |
| 293 |
|
| 294 |
.. hlist:: |
| 295 |
:columns: 4 |
| 296 |
|
| 297 |
- :ref:`Brokers <brokers>` |
| 298 |
- :ref:`Applications <guide-app>` |
| 299 |
- :ref:`Tasks <guide-tasks>` |
| 300 |
- :ref:`Calling <guide-calling>` |
| 301 |
- :ref:`Workers <guide-workers>` |
| 302 |
- :ref:`Daemonizing <daemonizing>` |
| 303 |
- :ref:`Monitoring <guide-monitoring>` |
| 304 |
- :ref:`Optimizing <guide-optimizing>` |
| 305 |
- :ref:`Security <guide-security>` |
| 306 |
- :ref:`Routing <guide-routing>` |
| 307 |
- :ref:`Configuration <configuration>` |
| 308 |
- :ref:`Django <django>` |
| 309 |
- :ref:`Contributing <contributing>` |
| 310 |
- :ref:`Signals <signals>` |
| 311 |
- :ref:`FAQ <faq>` |
| 312 |
- :ref:`API Reference <apiref>` |
| 313 |
|
| 314 |
.. include:: ../includes/installation.txt |