| 1 |
.. _faq: |
| 2 |
|
| 3 |
============================ |
| 4 |
Frequently Asked Questions |
| 5 |
============================ |
| 6 |
|
| 7 |
.. contents:: |
| 8 |
:local: |
| 9 |
|
| 10 |
.. _faq-general: |
| 11 |
|
| 12 |
General |
| 13 |
======= |
| 14 |
|
| 15 |
.. _faq-when-to-use: |
| 16 |
|
| 17 |
What kinds of things should I use Celery for? |
| 18 |
--------------------------------------------- |
| 19 |
|
| 20 |
**Answer:** `Queue everything and delight everyone`_ is a good article |
| 21 |
describing why you'd use a queue in a web context. |
| 22 |
|
| 23 |
.. _`Queue everything and delight everyone`: |
| 24 |
https://decafbad.com/blog/2008/07/04/queue-everything-and-delight-everyone |
| 25 |
|
| 26 |
These are some common use cases: |
| 27 |
|
| 28 |
* Running something in the background. For example, to finish the web request |
| 29 |
as soon as possible, then update the users page incrementally. |
| 30 |
This gives the user the impression of good performance and "snappiness", even |
| 31 |
though the real work might actually take some time. |
| 32 |
|
| 33 |
* Running something after the web request has finished. |
| 34 |
|
| 35 |
* Making sure something is done, by executing it asynchronously and using |
| 36 |
retries. |
| 37 |
|
| 38 |
* Scheduling periodic work. |
| 39 |
|
| 40 |
And to some degree: |
| 41 |
|
| 42 |
* Distributed computing. |
| 43 |
|
| 44 |
* Parallel execution. |
| 45 |
|
| 46 |
.. _faq-misconceptions: |
| 47 |
|
| 48 |
Misconceptions |
| 49 |
============== |
| 50 |
|
| 51 |
.. _faq-loc: |
| 52 |
|
| 53 |
Does Celery really consist of 50.000 lines of code? |
| 54 |
--------------------------------------------------- |
| 55 |
|
| 56 |
**Answer:** No, this and similarly large numbers have |
| 57 |
been reported at various locations. |
| 58 |
|
| 59 |
The numbers as of this writing are: |
| 60 |
|
| 61 |
- core: 7,141 lines of code. |
| 62 |
- tests: 14,209 lines. |
| 63 |
- backends, contrib, compat utilities: 9,032 lines. |
| 64 |
|
| 65 |
Lines of code isn't a useful metric, so |
| 66 |
even if Celery did consist of 50k lines of code you wouldn't |
| 67 |
be able to draw any conclusions from such a number. |
| 68 |
|
| 69 |
Does Celery have many dependencies? |
| 70 |
----------------------------------- |
| 71 |
|
| 72 |
A common criticism is that Celery uses too many dependencies. |
| 73 |
The rationale behind such a fear is hard to imagine, especially considering |
| 74 |
code reuse as the established way to combat complexity in modern software |
| 75 |
development, and that the cost of adding dependencies is very low now |
| 76 |
that package managers like pip and PyPI makes the hassle of installing |
| 77 |
and maintaining dependencies a thing of the past. |
| 78 |
|
| 79 |
Celery has replaced several dependencies along the way, and |
| 80 |
the current list of dependencies are: |
| 81 |
|
| 82 |
celery |
| 83 |
~~~~~~ |
| 84 |
|
| 85 |
- :pypi:`kombu` |
| 86 |
|
| 87 |
Kombu is part of the Celery ecosystem and is the library used |
| 88 |
to send and receive messages. It's also the library that enables |
| 89 |
us to support many different message brokers. It's also used by the |
| 90 |
OpenStack project, and many others, validating the choice to separate |
| 91 |
it from the Celery code-base. |
| 92 |
|
| 93 |
- :pypi:`billiard` |
| 94 |
|
| 95 |
Billiard is a fork of the Python multiprocessing module containing |
| 96 |
many performance and stability improvements. It's an eventual goal |
| 97 |
that these improvements will be merged back into Python one day. |
| 98 |
|
| 99 |
It's also used for compatibility with older Python versions |
| 100 |
that don't come with the multiprocessing module. |
| 101 |
|
| 102 |
kombu |
| 103 |
~~~~~ |
| 104 |
|
| 105 |
Kombu depends on the following packages: |
| 106 |
|
| 107 |
- :pypi:`amqp` |
| 108 |
|
| 109 |
The underlying pure-Python amqp client implementation. AMQP being the default |
| 110 |
broker this is a natural dependency. |
| 111 |
|
| 112 |
.. note:: |
| 113 |
|
| 114 |
To handle the dependencies for popular configuration |
| 115 |
choices Celery defines a number of "bundle" packages, |
| 116 |
see :ref:`bundles`. |
| 117 |
|
| 118 |
|
| 119 |
.. _faq-heavyweight: |
| 120 |
|
| 121 |
Is Celery heavy-weight? |
| 122 |
----------------------- |
| 123 |
|
| 124 |
Celery poses very little overhead both in memory footprint and |
| 125 |
performance. |
| 126 |
|
| 127 |
But please note that the default configuration isn't optimized for time nor |
| 128 |
space, see the :ref:`guide-optimizing` guide for more information. |
| 129 |
|
| 130 |
.. _faq-serialization-is-a-choice: |
| 131 |
|
| 132 |
Is Celery dependent on pickle? |
| 133 |
------------------------------ |
| 134 |
|
| 135 |
**Answer:** No, Celery can support any serialization scheme. |
| 136 |
|
| 137 |
We have built-in support for JSON, YAML, Pickle, and msgpack. |
| 138 |
Every task is associated with a content type, so you can even send one task using pickle, |
| 139 |
another using JSON. |
| 140 |
|
| 141 |
The default serialization support used to be pickle, but since 4.0 the default |
| 142 |
is now JSON. If you require sending complex Python objects as task arguments, |
| 143 |
you can use pickle as the serialization format, but see notes in |
| 144 |
:ref:`security-serializers`. |
| 145 |
|
| 146 |
If you need to communicate with other languages you should use |
| 147 |
a serialization format suited to that task, which pretty much means any |
| 148 |
serializer that's not pickle. |
| 149 |
|
| 150 |
You can set a global default serializer, the default serializer for a |
| 151 |
particular Task, or even what serializer to use when sending a single task |
| 152 |
instance. |
| 153 |
|
| 154 |
.. _faq-is-celery-for-django-only: |
| 155 |
|
| 156 |
Is Celery for Django only? |
| 157 |
-------------------------- |
| 158 |
|
| 159 |
**Answer:** No, you can use Celery with any framework, web or otherwise. |
| 160 |
|
| 161 |
.. _faq-is-celery-for-rabbitmq-only: |
| 162 |
|
| 163 |
Do I have to use AMQP/RabbitMQ? |
| 164 |
------------------------------- |
| 165 |
|
| 166 |
**Answer**: No, although using RabbitMQ is recommended you can also |
| 167 |
use Redis, SQS, or Qpid. |
| 168 |
|
| 169 |
See :ref:`brokers` for more information. |
| 170 |
|
| 171 |
Redis as a broker won't perform as well as |
| 172 |
an AMQP broker, but the combination RabbitMQ as broker and Redis as a result |
| 173 |
store is commonly used. If you have strict reliability requirements you're |
| 174 |
encouraged to use RabbitMQ or another AMQP broker. Some transports also use |
| 175 |
polling, so they're likely to consume more resources. However, if you for |
| 176 |
some reason aren't able to use AMQP, feel free to use these alternatives. |
| 177 |
They will probably work fine for most use cases, and note that the above |
| 178 |
points are not specific to Celery; If using Redis/database as a queue worked |
| 179 |
fine for you before, it probably will now. You can always upgrade later |
| 180 |
if you need to. |
| 181 |
|
| 182 |
.. _faq-is-celery-multilingual: |
| 183 |
|
| 184 |
Is Celery multilingual? |
| 185 |
------------------------ |
| 186 |
|
| 187 |
**Answer:** Yes. |
| 188 |
|
| 189 |
:mod:`~celery.bin.worker` is an implementation of Celery in Python. If the |
| 190 |
language has an AMQP client, there shouldn't be much work to create a worker |
| 191 |
in your language. A Celery worker is just a program connecting to the broker |
| 192 |
to process messages. |
| 193 |
|
| 194 |
Also, there's another way to be language-independent, and that's to use REST |
| 195 |
tasks, instead of your tasks being functions, they're URLs. With this |
| 196 |
information you can even create simple web servers that enable preloading of |
| 197 |
code. Simply expose an endpoint that performs an operation, and create a task |
| 198 |
that just performs an HTTP request to that endpoint. |
| 199 |
|
| 200 |
You can also use `Flower's <https://flower.readthedocs.io>`_ `REST API <https://flower.readthedocs.io/en/latest/api.html#post--api-task-async-apply-(.+)>`_ to invoke tasks. |
| 201 |
|
| 202 |
.. _faq-troubleshooting: |
| 203 |
|
| 204 |
Troubleshooting |
| 205 |
=============== |
| 206 |
|
| 207 |
.. _faq-mysql-deadlocks: |
| 208 |
|
| 209 |
MySQL is throwing deadlock errors, what can I do? |
| 210 |
------------------------------------------------- |
| 211 |
|
| 212 |
**Answer:** MySQL has default isolation level set to `REPEATABLE-READ`, |
| 213 |
if you don't really need that, set it to `READ-COMMITTED`. |
| 214 |
You can do that by adding the following to your :file:`my.cnf`:: |
| 215 |
|
| 216 |
[mysqld] |
| 217 |
transaction-isolation = READ-COMMITTED |
| 218 |
|
| 219 |
For more information about InnoDB’s transaction model see `MySQL - The InnoDB |
| 220 |
Transaction Model and Locking`_ in the MySQL user manual. |
| 221 |
|
| 222 |
(Thanks to Honza Kral and Anton Tsigularov for this solution) |
| 223 |
|
| 224 |
.. _`MySQL - The InnoDB Transaction Model and Locking`: https://dev.mysql.com/doc/refman/5.1/en/innodb-transaction-model.html |
| 225 |
|
| 226 |
.. _faq-worker-hanging: |
| 227 |
|
| 228 |
The worker isn't doing anything, just hanging |
| 229 |
--------------------------------------------- |
| 230 |
|
| 231 |
**Answer:** See `MySQL is throwing deadlock errors, what can I do?`_, |
| 232 |
or `Why is Task.delay/apply\*/the worker just hanging?`_. |
| 233 |
|
| 234 |
.. _faq-results-unreliable: |
| 235 |
|
| 236 |
Task results aren't reliably returning |
| 237 |
-------------------------------------- |
| 238 |
|
| 239 |
**Answer:** If you're using the database backend for results, and in particular |
| 240 |
using MySQL, see `MySQL is throwing deadlock errors, what can I do?`_. |
| 241 |
|
| 242 |
.. _faq-publish-hanging: |
| 243 |
|
| 244 |
Why is Task.delay/apply\*/the worker just hanging? |
| 245 |
-------------------------------------------------- |
| 246 |
|
| 247 |
**Answer:** There's a bug in some AMQP clients that'll make it hang if |
| 248 |
it's not able to authenticate the current user, the password doesn't match or |
| 249 |
the user doesn't have access to the virtual host specified. Be sure to check |
| 250 |
your broker logs (for RabbitMQ that's :file:`/var/log/rabbitmq/rabbit.log` on |
| 251 |
most systems), it usually contains a message describing the reason. |
| 252 |
|
| 253 |
.. _faq-worker-on-freebsd: |
| 254 |
|
| 255 |
Does it work on FreeBSD? |
| 256 |
------------------------ |
| 257 |
|
| 258 |
**Answer:** Depends; |
| 259 |
|
| 260 |
When using the RabbitMQ (AMQP) and Redis transports it should work |
| 261 |
out of the box. |
| 262 |
|
| 263 |
For other transports the compatibility prefork pool is |
| 264 |
used and requires a working POSIX semaphore implementation, |
| 265 |
this is enabled in FreeBSD by default since FreeBSD 8.x. |
| 266 |
For older version of FreeBSD, you have to enable |
| 267 |
POSIX semaphores in the kernel and manually recompile billiard. |
| 268 |
|
| 269 |
Luckily, Viktor Petersson has written a tutorial to get you started with |
| 270 |
Celery on FreeBSD here: |
| 271 |
http://www.playingwithwire.com/2009/10/how-to-get-celeryd-to-work-on-freebsd/ |
| 272 |
|
| 273 |
.. _faq-duplicate-key-errors: |
| 274 |
|
| 275 |
I'm having `IntegrityError: Duplicate Key` errors. Why? |
| 276 |
--------------------------------------------------------- |
| 277 |
|
| 278 |
**Answer:** See `MySQL is throwing deadlock errors, what can I do?`_. |
| 279 |
Thanks to :github_user:`@howsthedotcom`. |
| 280 |
|
| 281 |
.. _faq-worker-stops-processing: |
| 282 |
|
| 283 |
Why aren't my tasks processed? |
| 284 |
------------------------------ |
| 285 |
|
| 286 |
**Answer:** With RabbitMQ you can see how many consumers are currently |
| 287 |
receiving tasks by running the following command: |
| 288 |
|
| 289 |
.. code-block:: console |
| 290 |
|
| 291 |
$ rabbitmqctl list_queues -p <myvhost> name messages consumers |
| 292 |
Listing queues ... |
| 293 |
celery 2891 2 |
| 294 |
|
| 295 |
This shows that there's 2891 messages waiting to be processed in the task |
| 296 |
queue, and there are two consumers processing them. |
| 297 |
|
| 298 |
One reason that the queue is never emptied could be that you have a stale |
| 299 |
worker process taking the messages hostage. This could happen if the worker |
| 300 |
wasn't properly shut down. |
| 301 |
|
| 302 |
When a message is received by a worker the broker waits for it to be |
| 303 |
acknowledged before marking the message as processed. The broker won't |
| 304 |
re-send that message to another consumer until the consumer is shut down |
| 305 |
properly. |
| 306 |
|
| 307 |
If you hit this problem you have to kill all workers manually and restart |
| 308 |
them: |
| 309 |
|
| 310 |
.. code-block:: console |
| 311 |
|
| 312 |
$ pkill 'celery worker' |
| 313 |
|
| 314 |
$ # - If you don't have pkill use: |
| 315 |
$ # ps auxww | awk '/celery worker/ {print $2}' | xargs kill |
| 316 |
|
| 317 |
You may have to wait a while until all workers have finished executing |
| 318 |
tasks. If it's still hanging after a long time you can kill them by force |
| 319 |
with: |
| 320 |
|
| 321 |
.. code-block:: console |
| 322 |
|
| 323 |
$ pkill -9 'celery worker' |
| 324 |
|
| 325 |
$ # - If you don't have pkill use: |
| 326 |
$ # ps auxww | awk '/celery worker/ {print $2}' | xargs kill -9 |
| 327 |
|
| 328 |
.. _faq-task-does-not-run: |
| 329 |
|
| 330 |
Why won't my Task run? |
| 331 |
---------------------- |
| 332 |
|
| 333 |
**Answer:** There might be syntax errors preventing the tasks module being imported. |
| 334 |
|
| 335 |
You can find out if Celery is able to run the task by executing the |
| 336 |
task manually: |
| 337 |
|
| 338 |
.. code-block:: python |
| 339 |
|
| 340 |
>>> from myapp.tasks import MyPeriodicTask |
| 341 |
>>> MyPeriodicTask.delay() |
| 342 |
|
| 343 |
Watch the workers log file to see if it's able to find the task, or if some |
| 344 |
other error is happening. |
| 345 |
|
| 346 |
.. _faq-periodic-task-does-not-run: |
| 347 |
|
| 348 |
Why won't my periodic task run? |
| 349 |
------------------------------- |
| 350 |
|
| 351 |
**Answer:** See `Why won't my Task run?`_. |
| 352 |
|
| 353 |
.. _faq-purge-the-queue: |
| 354 |
|
| 355 |
How do I purge all waiting tasks? |
| 356 |
--------------------------------- |
| 357 |
|
| 358 |
**Answer:** You can use the ``celery purge`` command to purge |
| 359 |
all configured task queues: |
| 360 |
|
| 361 |
.. code-block:: console |
| 362 |
|
| 363 |
$ celery -A proj purge |
| 364 |
|
| 365 |
or programmatically: |
| 366 |
|
| 367 |
.. code-block:: pycon |
| 368 |
|
| 369 |
>>> from proj.celery import app |
| 370 |
>>> app.control.purge() |
| 371 |
1753 |
| 372 |
|
| 373 |
If you only want to purge messages from a specific queue |
| 374 |
you have to use the AMQP API or the :program:`celery amqp` utility: |
| 375 |
|
| 376 |
.. code-block:: console |
| 377 |
|
| 378 |
$ celery -A proj amqp queue.purge <queue name> |
| 379 |
|
| 380 |
The number 1753 is the number of messages deleted. |
| 381 |
|
| 382 |
You can also start the worker with the |
| 383 |
:option:`--purge <celery worker --purge>` option enabled to purge messages |
| 384 |
when the worker starts. |
| 385 |
|
| 386 |
.. _faq-messages-left-after-purge: |
| 387 |
|
| 388 |
I've purged messages, but there are still messages left in the queue? |
| 389 |
--------------------------------------------------------------------- |
| 390 |
|
| 391 |
**Answer:** Tasks are acknowledged (removed from the queue) as soon |
| 392 |
as they're actually executed. After the worker has received a task, it will |
| 393 |
take some time until it's actually executed, especially if there are a lot |
| 394 |
of tasks already waiting for execution. Messages that aren't acknowledged are |
| 395 |
held on to by the worker until it closes the connection to the broker (AMQP |
| 396 |
server). When that connection is closed (e.g., because the worker was stopped) |
| 397 |
the tasks will be re-sent by the broker to the next available worker (or the |
| 398 |
same worker when it has been restarted), so to properly purge the queue of |
| 399 |
waiting tasks you have to stop all the workers, and then purge the tasks |
| 400 |
using :func:`celery.control.purge`. |
| 401 |
|
| 402 |
.. _faq-results: |
| 403 |
|
| 404 |
Results |
| 405 |
======= |
| 406 |
|
| 407 |
.. _faq-get-result-by-task-id: |
| 408 |
|
| 409 |
How do I get the result of a task if I have the ID that points there? |
| 410 |
---------------------------------------------------------------------- |
| 411 |
|
| 412 |
**Answer**: Use `task.AsyncResult`: |
| 413 |
|
| 414 |
.. code-block:: pycon |
| 415 |
|
| 416 |
>>> result = my_task.AsyncResult(task_id) |
| 417 |
>>> result.get() |
| 418 |
|
| 419 |
This will give you a :class:`~celery.result.AsyncResult` instance |
| 420 |
using the tasks current result backend. |
| 421 |
|
| 422 |
If you need to specify a custom result backend, or you want to use |
| 423 |
the current application's default backend you can use |
| 424 |
:class:`@AsyncResult`: |
| 425 |
|
| 426 |
.. code-block:: pycon |
| 427 |
|
| 428 |
>>> result = app.AsyncResult(task_id) |
| 429 |
>>> result.get() |
| 430 |
|
| 431 |
.. _faq-security: |
| 432 |
|
| 433 |
Security |
| 434 |
======== |
| 435 |
|
| 436 |
Isn't using `pickle` a security concern? |
| 437 |
---------------------------------------- |
| 438 |
|
| 439 |
**Answer**: Indeed, since Celery 4.0 the default serializer is now JSON |
| 440 |
to make sure people are choosing serializers consciously and aware of this concern. |
| 441 |
|
| 442 |
It's essential that you protect against unauthorized |
| 443 |
access to your broker, databases and other services transmitting pickled |
| 444 |
data. |
| 445 |
|
| 446 |
Note that this isn't just something you should be aware of with Celery, for |
| 447 |
example also Django uses pickle for its cache client. |
| 448 |
|
| 449 |
For the task messages you can set the :setting:`task_serializer` |
| 450 |
setting to "json" or "yaml" instead of pickle. |
| 451 |
|
| 452 |
Similarly for task results you can set :setting:`result_serializer`. |
| 453 |
|
| 454 |
For more details of the formats used and the lookup order when |
| 455 |
checking what format to use for a task see :ref:`calling-serializers` |
| 456 |
|
| 457 |
Can messages be encrypted? |
| 458 |
-------------------------- |
| 459 |
|
| 460 |
**Answer**: Some AMQP brokers supports using SSL (including RabbitMQ). |
| 461 |
You can enable this using the :setting:`broker_use_ssl` setting. |
| 462 |
|
| 463 |
It's also possible to add additional encryption and security to messages, |
| 464 |
if you have a need for this then you should contact the :ref:`mailing-list`. |
| 465 |
|
| 466 |
Is it safe to run :program:`celery worker` as root? |
| 467 |
--------------------------------------------------- |
| 468 |
|
| 469 |
**Answer**: No! |
| 470 |
|
| 471 |
We're not currently aware of any security issues, but it would |
| 472 |
be incredibly naive to assume that they don't exist, so running |
| 473 |
the Celery services (:program:`celery worker`, :program:`celery beat`, |
| 474 |
:program:`celeryev`, etc) as an unprivileged user is recommended. |
| 475 |
|
| 476 |
.. _faq-brokers: |
| 477 |
|
| 478 |
Brokers |
| 479 |
======= |
| 480 |
|
| 481 |
Why is RabbitMQ crashing? |
| 482 |
------------------------- |
| 483 |
|
| 484 |
**Answer:** RabbitMQ will crash if it runs out of memory. This will be fixed in a |
| 485 |
future release of RabbitMQ. please refer to the RabbitMQ FAQ: |
| 486 |
https://www.rabbitmq.com/faq.html#node-runs-out-of-memory |
| 487 |
|
| 488 |
.. note:: |
| 489 |
|
| 490 |
This is no longer the case, RabbitMQ versions 2.0 and above |
| 491 |
includes a new persister, that's tolerant to out of memory |
| 492 |
errors. RabbitMQ 2.1 or higher is recommended for Celery. |
| 493 |
|
| 494 |
If you're still running an older version of RabbitMQ and experience |
| 495 |
crashes, then please upgrade! |
| 496 |
|
| 497 |
Misconfiguration of Celery can eventually lead to a crash |
| 498 |
on older version of RabbitMQ. Even if it doesn't crash, this |
| 499 |
can still consume a lot of resources, so it's |
| 500 |
important that you're aware of the common pitfalls. |
| 501 |
|
| 502 |
* Events. |
| 503 |
|
| 504 |
Running :mod:`~celery.bin.worker` with the :option:`-E <celery worker -E>` |
| 505 |
option will send messages for events happening inside of the worker. |
| 506 |
|
| 507 |
Events should only be enabled if you have an active monitor consuming them, |
| 508 |
or if you purge the event queue periodically. |
| 509 |
|
| 510 |
* AMQP backend results. |
| 511 |
|
| 512 |
When running with the AMQP result backend, every task result will be sent |
| 513 |
as a message. If you don't collect these results, they will build up and |
| 514 |
RabbitMQ will eventually run out of memory. |
| 515 |
|
| 516 |
This result backend is now deprecated so you shouldn't be using it. |
| 517 |
Use either the RPC backend for rpc-style calls, or a persistent backend |
| 518 |
if you need multi-consumer access to results. |
| 519 |
|
| 520 |
Results expire after 1 day by default. It may be a good idea |
| 521 |
to lower this value by configuring the :setting:`result_expires` |
| 522 |
setting. |
| 523 |
|
| 524 |
If you don't use the results for a task, make sure you set the |
| 525 |
`ignore_result` option: |
| 526 |
|
| 527 |
.. code-block:: python |
| 528 |
|
| 529 |
@app.task(ignore_result=True) |
| 530 |
def mytask(): |
| 531 |
pass |
| 532 |
|
| 533 |
class MyTask(Task): |
| 534 |
ignore_result = True |
| 535 |
|
| 536 |
.. _faq-use-celery-with-stomp: |
| 537 |
|
| 538 |
Can I use Celery with ActiveMQ/STOMP? |
| 539 |
------------------------------------- |
| 540 |
|
| 541 |
**Answer**: No. It used to be supported by :pypi:`Carrot` (our old messaging library) |
| 542 |
but isn't currently supported in :pypi:`Kombu` (our new messaging library). |
| 543 |
|
| 544 |
.. _faq-non-amqp-missing-features: |
| 545 |
|
| 546 |
What features aren't supported when not using an AMQP broker? |
| 547 |
------------------------------------------------------------- |
| 548 |
|
| 549 |
This is an incomplete list of features not available when |
| 550 |
using the virtual transports: |
| 551 |
|
| 552 |
* Remote control commands (supported only by Redis). |
| 553 |
|
| 554 |
* Monitoring with events may not work in all virtual transports. |
| 555 |
|
| 556 |
* The `header` and `fanout` exchange types |
| 557 |
(`fanout` is supported by Redis). |
| 558 |
|
| 559 |
.. _faq-tasks: |
| 560 |
|
| 561 |
Tasks |
| 562 |
===== |
| 563 |
|
| 564 |
.. _faq-tasks-connection-reuse: |
| 565 |
|
| 566 |
How can I reuse the same connection when calling tasks? |
| 567 |
------------------------------------------------------- |
| 568 |
|
| 569 |
**Answer**: See the :setting:`broker_pool_limit` setting. |
| 570 |
The connection pool is enabled by default since version 2.5. |
| 571 |
|
| 572 |
.. _faq-sudo-subprocess: |
| 573 |
|
| 574 |
:command:`sudo` in a :mod:`subprocess` returns :const:`None` |
| 575 |
------------------------------------------------------------ |
| 576 |
|
| 577 |
There's a :command:`sudo` configuration option that makes it illegal |
| 578 |
for process without a tty to run :command:`sudo`: |
| 579 |
|
| 580 |
.. code-block:: text |
| 581 |
|
| 582 |
Defaults requiretty |
| 583 |
|
| 584 |
If you have this configuration in your :file:`/etc/sudoers` file then |
| 585 |
tasks won't be able to call :command:`sudo` when the worker is |
| 586 |
running as a daemon. If you want to enable that, then you need to remove |
| 587 |
the line from :file:`/etc/sudoers`. |
| 588 |
|
| 589 |
See: http://timelordz.com/wiki/Apache_Sudo_Commands |
| 590 |
|
| 591 |
.. _faq-deletes-unknown-tasks: |
| 592 |
|
| 593 |
Why do workers delete tasks from the queue if they're unable to process them? |
| 594 |
----------------------------------------------------------------------------- |
| 595 |
**Answer**: |
| 596 |
|
| 597 |
The worker rejects unknown tasks, messages with encoding errors and messages |
| 598 |
that don't contain the proper fields (as per the task message protocol). |
| 599 |
|
| 600 |
If it didn't reject them they could be redelivered again and again, |
| 601 |
causing a loop. |
| 602 |
|
| 603 |
Recent versions of RabbitMQ has the ability to configure a dead-letter |
| 604 |
queue for exchange, so that rejected messages is moved there. |
| 605 |
|
| 606 |
.. _faq-execute-task-by-name: |
| 607 |
|
| 608 |
Can I call a task by name? |
| 609 |
----------------------------- |
| 610 |
|
| 611 |
**Answer**: Yes, use :meth:`@send_task`. |
| 612 |
|
| 613 |
You can also call a task by name, from any language, |
| 614 |
using an AMQP client: |
| 615 |
|
| 616 |
.. code-block:: python |
| 617 |
|
| 618 |
>>> app.send_task('tasks.add', args=[2, 2], kwargs={}) |
| 619 |
<AsyncResult: 373550e8-b9a0-4666-bc61-ace01fa4f91d> |
| 620 |
|
| 621 |
To use ``chain``, ``chord`` or ``group`` with tasks called by name, |
| 622 |
use the :meth:`@Celery.signature` method: |
| 623 |
|
| 624 |
.. code-block:: python |
| 625 |
|
| 626 |
>>> chain( |
| 627 |
... app.signature('tasks.add', args=[2, 2], kwargs={}), |
| 628 |
... app.signature('tasks.add', args=[1, 1], kwargs={}) |
| 629 |
... ).apply_async() |
| 630 |
<AsyncResult: e9d52312-c161-46f0-9013-2713e6df812d> |
| 631 |
|
| 632 |
.. _faq-get-current-task-id: |
| 633 |
|
| 634 |
Can I get the task id of the current task? |
| 635 |
---------------------------------------------- |
| 636 |
|
| 637 |
**Answer**: Yes, the current id and more is available in the task request:: |
| 638 |
|
| 639 |
@app.task(bind=True) |
| 640 |
def mytask(self): |
| 641 |
cache.set(self.request.id, "Running") |
| 642 |
|
| 643 |
For more information see :ref:`task-request-info`. |
| 644 |
|
| 645 |
If you don't have a reference to the task instance you can use |
| 646 |
:attr:`app.current_task <@current_task>`: |
| 647 |
|
| 648 |
.. code-block:: python |
| 649 |
|
| 650 |
>>> app.current_task.request.id |
| 651 |
|
| 652 |
But note that this will be any task, be it one executed by the worker, or a |
| 653 |
task called directly by that task, or a task called eagerly. |
| 654 |
|
| 655 |
To get the current task being worked on specifically, use |
| 656 |
:attr:`app.current_worker_task <@current_worker_task>`: |
| 657 |
|
| 658 |
.. code-block:: python |
| 659 |
|
| 660 |
>>> app.current_worker_task.request.id |
| 661 |
|
| 662 |
.. note:: |
| 663 |
|
| 664 |
Both :attr:`~@current_task`, and :attr:`~@current_worker_task` can be |
| 665 |
:const:`None`. |
| 666 |
|
| 667 |
.. _faq-custom-task-ids: |
| 668 |
|
| 669 |
Can I specify a custom task_id? |
| 670 |
------------------------------- |
| 671 |
|
| 672 |
**Answer**: Yes, use the `task_id` argument to :meth:`Task.apply_async`: |
| 673 |
|
| 674 |
.. code-block:: pycon |
| 675 |
|
| 676 |
>>> task.apply_async(args, kwargs, task_id='…') |
| 677 |
|
| 678 |
|
| 679 |
Can I use decorators with tasks? |
| 680 |
-------------------------------- |
| 681 |
|
| 682 |
**Answer**: Yes, but please see note in the sidebar at :ref:`task-basics`. |
| 683 |
|
| 684 |
.. _faq-natural-task-ids: |
| 685 |
|
| 686 |
Can I use natural task ids? |
| 687 |
--------------------------- |
| 688 |
|
| 689 |
**Answer**: Yes, but make sure it's unique, as the behavior |
| 690 |
for two tasks existing with the same id is undefined. |
| 691 |
|
| 692 |
The world will probably not explode, but they can |
| 693 |
definitely overwrite each others results. |
| 694 |
|
| 695 |
.. _faq-task-callbacks: |
| 696 |
|
| 697 |
Can I run a task once another task has finished? |
| 698 |
------------------------------------------------ |
| 699 |
|
| 700 |
**Answer**: Yes, you can safely launch a task inside a task. |
| 701 |
|
| 702 |
A common pattern is to add callbacks to tasks: |
| 703 |
|
| 704 |
.. code-block:: python |
| 705 |
|
| 706 |
from celery.utils.log import get_task_logger |
| 707 |
|
| 708 |
logger = get_task_logger(__name__) |
| 709 |
|
| 710 |
@app.task |
| 711 |
def add(x, y): |
| 712 |
return x + y |
| 713 |
|
| 714 |
@app.task(ignore_result=True) |
| 715 |
def log_result(result): |
| 716 |
logger.info("log_result got: %r", result) |
| 717 |
|
| 718 |
Invocation: |
| 719 |
|
| 720 |
.. code-block:: pycon |
| 721 |
|
| 722 |
>>> (add.s(2, 2) | log_result.s()).delay() |
| 723 |
|
| 724 |
See :doc:`userguide/canvas` for more information. |
| 725 |
|
| 726 |
.. _faq-cancel-task: |
| 727 |
|
| 728 |
Can I cancel the execution of a task? |
| 729 |
------------------------------------- |
| 730 |
**Answer**: Yes, Use :meth:`result.revoke() <celery.result.AsyncResult.revoke>`: |
| 731 |
|
| 732 |
.. code-block:: pycon |
| 733 |
|
| 734 |
>>> result = add.apply_async(args=[2, 2], countdown=120) |
| 735 |
>>> result.revoke() |
| 736 |
|
| 737 |
or if you only have the task id: |
| 738 |
|
| 739 |
.. code-block:: pycon |
| 740 |
|
| 741 |
>>> from proj.celery import app |
| 742 |
>>> app.control.revoke(task_id) |
| 743 |
|
| 744 |
|
| 745 |
The latter also support passing a list of task-ids as argument. |
| 746 |
|
| 747 |
.. _faq-node-not-receiving-broadcast-commands: |
| 748 |
|
| 749 |
Why aren't my remote control commands received by all workers? |
| 750 |
-------------------------------------------------------------- |
| 751 |
|
| 752 |
**Answer**: To receive broadcast remote control commands, every worker node |
| 753 |
creates a unique queue name, based on the nodename of the worker. |
| 754 |
|
| 755 |
If you have more than one worker with the same host name, the |
| 756 |
control commands will be received in round-robin between them. |
| 757 |
|
| 758 |
To work around this you can explicitly set the nodename for every worker |
| 759 |
using the :option:`-n <celery worker -n>` argument to |
| 760 |
:mod:`~celery.bin.worker`: |
| 761 |
|
| 762 |
.. code-block:: console |
| 763 |
|
| 764 |
$ celery -A proj worker -n worker1@%h |
| 765 |
$ celery -A proj worker -n worker2@%h |
| 766 |
|
| 767 |
where ``%h`` expands into the current hostname. |
| 768 |
|
| 769 |
.. _faq-task-routing: |
| 770 |
|
| 771 |
Can I send some tasks to only some servers? |
| 772 |
-------------------------------------------- |
| 773 |
|
| 774 |
**Answer:** Yes, you can route tasks to one or more workers, |
| 775 |
using different message routing topologies, and a worker instance |
| 776 |
can bind to multiple queues. |
| 777 |
|
| 778 |
See :doc:`userguide/routing` for more information. |
| 779 |
|
| 780 |
.. _faq-disable-prefetch: |
| 781 |
|
| 782 |
Can I disable prefetching of tasks? |
| 783 |
----------------------------------- |
| 784 |
|
| 785 |
**Answer**: Maybe! The AMQP term "prefetch" is confusing, as it's only used |
| 786 |
to describe the task prefetching *limit*. There's no actual prefetching involved. |
| 787 |
|
| 788 |
Disabling the prefetch limits is possible, but that means the worker will |
| 789 |
consume as many tasks as it can, as fast as possible. |
| 790 |
|
| 791 |
You can use the :option:`--disable-prefetch <celery worker --disable-prefetch>` |
| 792 |
flag (or set :setting:`worker_disable_prefetch` to ``True``) so that a worker |
| 793 |
only fetches a task when one of its processes is free. This feature is currently |
| 794 |
only supported when using Redis as the broker. |
| 795 |
|
| 796 |
A discussion on prefetch limits, and configuration settings for a worker |
| 797 |
that only reserves one task at a time is found here: |
| 798 |
:ref:`optimizing-prefetch-limit`. |
| 799 |
|
| 800 |
.. _faq-change-periodic-task-interval-at-runtime: |
| 801 |
|
| 802 |
Can I change the interval of a periodic task at runtime? |
| 803 |
-------------------------------------------------------- |
| 804 |
|
| 805 |
**Answer**: Yes, you can use the Django database scheduler, or you can |
| 806 |
create a new schedule subclass and override |
| 807 |
:meth:`~celery.schedules.schedule.is_due`: |
| 808 |
|
| 809 |
.. code-block:: python |
| 810 |
|
| 811 |
from celery.schedules import schedule |
| 812 |
|
| 813 |
class my_schedule(schedule): |
| 814 |
|
| 815 |
def is_due(self, last_run_at): |
| 816 |
return run_now, next_time_to_check |
| 817 |
|
| 818 |
.. _faq-task-priorities: |
| 819 |
|
| 820 |
Does Celery support task priorities? |
| 821 |
------------------------------------ |
| 822 |
|
| 823 |
**Answer**: Yes, RabbitMQ supports priorities since version 3.5.0, |
| 824 |
and the Redis transport emulates priority support. |
| 825 |
|
| 826 |
You can also prioritize work by routing high priority tasks |
| 827 |
to different workers. In the real world this usually works better |
| 828 |
than per message priorities. You can use this in combination with rate |
| 829 |
limiting, and per message priorities to achieve a responsive system. |
| 830 |
|
| 831 |
.. _faq-acks_late-vs-retry: |
| 832 |
|
| 833 |
Should I use retry or acks_late? |
| 834 |
-------------------------------- |
| 835 |
|
| 836 |
**Answer**: Depends. It's not necessarily one or the other, you may want |
| 837 |
to use both. |
| 838 |
|
| 839 |
`Task.retry` is used to retry tasks, notably for expected errors that |
| 840 |
is catch-able with the :keyword:`try` block. The AMQP transaction isn't used |
| 841 |
for these errors: **if the task raises an exception it's still acknowledged!** |
| 842 |
|
| 843 |
The `acks_late` setting would be used when you need the task to be |
| 844 |
executed again if the worker (for some reason) crashes mid-execution. |
| 845 |
It's important to note that the worker isn't known to crash, and if |
| 846 |
it does it's usually an unrecoverable error that requires human |
| 847 |
intervention (bug in the worker, or task code). |
| 848 |
|
| 849 |
In an ideal world you could safely retry any task that's failed, but |
| 850 |
this is rarely the case. Imagine the following task: |
| 851 |
|
| 852 |
.. code-block:: python |
| 853 |
|
| 854 |
@app.task |
| 855 |
def process_upload(filename, tmpfile): |
| 856 |
# Increment a file count stored in a database |
| 857 |
increment_file_counter() |
| 858 |
add_file_metadata_to_db(filename, tmpfile) |
| 859 |
copy_file_to_destination(filename, tmpfile) |
| 860 |
|
| 861 |
If this crashed in the middle of copying the file to its destination |
| 862 |
the world would contain incomplete state. This isn't a critical |
| 863 |
scenario of course, but you can probably imagine something far more |
| 864 |
sinister. So for ease of programming we have less reliability; |
| 865 |
It's a good default, users who require it and know what they |
| 866 |
are doing can still enable acks_late (and in the future hopefully |
| 867 |
use manual acknowledgment). |
| 868 |
|
| 869 |
In addition `Task.retry` has features not available in AMQP |
| 870 |
transactions: delay between retries, max retries, etc. |
| 871 |
|
| 872 |
So use retry for Python errors, and if your task is idempotent |
| 873 |
combine that with `acks_late` if that level of reliability |
| 874 |
is required. |
| 875 |
|
| 876 |
.. _faq-schedule-at-specific-time: |
| 877 |
|
| 878 |
Can I schedule tasks to execute at a specific time? |
| 879 |
--------------------------------------------------- |
| 880 |
|
| 881 |
**Answer**: Yes. You can use the `eta` argument of :meth:`Task.apply_async`. |
| 882 |
Note that using distant `eta` times is not recommended, and in such case |
| 883 |
:ref:`periodic tasks<guide-beat>` should be preferred. |
| 884 |
|
| 885 |
See :ref:`calling-eta` for more details. |
| 886 |
|
| 887 |
.. _faq-safe-worker-shutdown: |
| 888 |
|
| 889 |
Can I safely shut down the worker? |
| 890 |
---------------------------------- |
| 891 |
|
| 892 |
**Answer**: Yes, use the :sig:`TERM` signal. |
| 893 |
|
| 894 |
This will tell the worker to finish all currently |
| 895 |
executing jobs and shut down as soon as possible. No tasks should be lost |
| 896 |
even with experimental transports as long as the shutdown completes. |
| 897 |
|
| 898 |
You should never stop :mod:`~celery.bin.worker` with the :sig:`KILL` signal |
| 899 |
(``kill -9``), unless you've tried :sig:`TERM` a few times and waited a few |
| 900 |
minutes to let it get a chance to shut down. |
| 901 |
|
| 902 |
Also make sure you kill the main worker process only, not any of its child |
| 903 |
processes. You can direct a kill signal to a specific child process if |
| 904 |
you know the process is currently executing a task the worker shutdown |
| 905 |
is depending on, but this also means that a ``WorkerLostError`` state will |
| 906 |
be set for the task so the task won't run again. |
| 907 |
|
| 908 |
Identifying the type of process is easier if you have installed the |
| 909 |
:pypi:`setproctitle` module: |
| 910 |
|
| 911 |
.. code-block:: console |
| 912 |
|
| 913 |
$ pip install setproctitle |
| 914 |
|
| 915 |
With this library installed you'll be able to see the type of process in |
| 916 |
:command:`ps` listings, but the worker must be restarted for this to take effect. |
| 917 |
|
| 918 |
.. seealso:: |
| 919 |
|
| 920 |
:ref:`worker-stopping` |
| 921 |
|
| 922 |
.. _faq-daemonizing: |
| 923 |
|
| 924 |
Can I run the worker in the background on [platform]? |
| 925 |
----------------------------------------------------- |
| 926 |
**Answer**: Yes, please see :ref:`daemonizing`. |
| 927 |
|
| 928 |
.. _faq-django: |
| 929 |
|
| 930 |
Django |
| 931 |
====== |
| 932 |
|
| 933 |
.. _faq-django-beat-database-tables: |
| 934 |
|
| 935 |
What purpose does the database tables created by ``django-celery-beat`` have? |
| 936 |
----------------------------------------------------------------------------- |
| 937 |
|
| 938 |
When the database-backed schedule is used the periodic task |
| 939 |
schedule is taken from the ``PeriodicTask`` model, there are |
| 940 |
also several other helper tables (``IntervalSchedule``, |
| 941 |
``CrontabSchedule``, ``PeriodicTasks``). |
| 942 |
|
| 943 |
.. _faq-django-result-database-tables: |
| 944 |
|
| 945 |
What purpose does the database tables created by ``django-celery-results`` have? |
| 946 |
-------------------------------------------------------------------------------- |
| 947 |
|
| 948 |
The Django database result backend extension requires |
| 949 |
two extra models: ``TaskResult`` and ``GroupResult``. |
| 950 |
|
| 951 |
.. _faq-windows: |
| 952 |
|
| 953 |
Windows |
| 954 |
======= |
| 955 |
|
| 956 |
.. _faq-windows-worker-embedded-beat: |
| 957 |
|
| 958 |
Does Celery support Windows? |
| 959 |
---------------------------------------------------------------- |
| 960 |
**Answer**: No. |
| 961 |
|
| 962 |
Since Celery 4.x, Windows is no longer supported due to lack of resources. |
| 963 |
|
| 964 |
But it may still work and we are happy to accept patches. |