FAQ 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. ============================
  2. Frequently Asked Questions
  3. ============================
  4. .. contents::
  5. :local:
  6. General
  7. =======
  8. What kinds of things should I use celery for?
  9. ---------------------------------------------
  10. **Answer:** `Queue everything and delight everyone`_ is a good article
  11. describing why you would use a queue in a web context.
  12. .. _`Queue everything and delight everyone`:
  13. http://decafbad.com/blog/2008/07/04/queue-everything-and-delight-everyone
  14. These are some common use cases:
  15. * Running something in the background. For example, to finish the web request
  16. as soon as possible, then update the users page incrementally.
  17. This gives the user the impression of good performane and "snappiness", even
  18. though the real work might actually take some time.
  19. * Running something after the web request has finished.
  20. * Making sure something is done, by executing it asynchronously and using
  21. retries.
  22. * Scheduling periodic work.
  23. And to some degree:
  24. * Distributed computing.
  25. * Parallel execution.
  26. Misconceptions
  27. ==============
  28. Is celery dependent on pickle?
  29. ------------------------------
  30. **Answer:** No.
  31. Celery can support any serialization scheme and has support for JSON/YAML and
  32. Pickle by default. You can even send one task using pickle, and another one
  33. with JSON seamlessly, this is because every task is associated with a
  34. content-type. The default serialization scheme is pickle because it's the most
  35. used, and it has support for sending complex objects as task arguments.
  36. You can set a global default serializer, the default serializer for a
  37. particular Task, or even what serializer to use when sending a single task
  38. instance.
  39. Is celery for Django only?
  40. --------------------------
  41. **Answer:** No.
  42. Celery does not depend on Django anymore. To use Celery with Django you have
  43. to use the `django-celery`_ package:
  44. .. _`django-celery`: http://pypi.python.org/pypi/django-celery
  45. Do I have to use AMQP/RabbitMQ?
  46. -------------------------------
  47. **Answer**: No.
  48. You can also use Redis or an SQL database, see `Using other
  49. queues`_.
  50. .. _`Using other queues`:
  51. http://ask.github.com/celery/tutorials/otherqueues.html
  52. Redis or a database won't perform as well as
  53. an AMQP broker. If you have strict reliability requirements you are
  54. encouraged to use RabbitMQ or another AMQP broker. Redis/database also use
  55. polling, so they are likely to consume more resources. However, if you for
  56. some reason are not able to use AMQP, feel free to use these alternatives.
  57. They will probably work fine for most use cases, and note that the above
  58. points are not specific to celery; If using Redis/database as a queue worked
  59. fine for you before, it probably will now. You can always upgrade later
  60. if you need to.
  61. Is celery multi-lingual?
  62. ------------------------
  63. **Answer:** Yes.
  64. celeryd is an implementation of celery in python. If the language has an AMQP
  65. client, there shouldn't be much work to create a worker in your language.
  66. A celery worker is just a program connecting to the broker to consume
  67. messages. There's no other communication involved.
  68. Also, there's another way to be language indepedent, and that is to use REST
  69. tasks, instead of your tasks being functions, they're URLs. With this
  70. information you can even create simple web servers that enable preloading of
  71. code. See: `User Guide: Remote Tasks`_.
  72. .. _`User Guide: Remote Tasks`:
  73. http://ask.github.com/celery/userguide/remote-tasks.html
  74. Troubleshooting
  75. ===============
  76. MySQL is throwing deadlock errors, what can I do?
  77. -------------------------------------------------
  78. **Answer:** MySQL has default isolation level set to ``REPEATABLE-READ``,
  79. if you don't really need that, set it to ``READ-COMMITTED``.
  80. You can do that by adding the following to your ``my.cnf``::
  81. [mysqld]
  82. transaction-isolation = READ-COMMITTED
  83. For more information about InnoDBs transaction model see `MySQL - The InnoDB
  84. Transaction Model and Locking`_ in the MySQL user manual.
  85. (Thanks to Honza Kral and Anton Tsigularov for this solution)
  86. .. _`MySQL - The InnoDB Transaction Model and Locking`: http://dev.mysql.com/doc/refman/5.1/en/innodb-transaction-model.html
  87. celeryd is not doing anything, just hanging
  88. --------------------------------------------
  89. **Answer:** See `MySQL is throwing deadlock errors, what can I do?`_.
  90. or `Why is Task.delay/apply\* just hanging?`.
  91. Why is Task.delay/apply\*/celeryd just hanging?
  92. -----------------------------------------------
  93. **Answer:** There is a bug in some AMQP clients that will make it hang if
  94. it's not able to authenticate the current user, the password doesn't match or
  95. the user does not have access to the virtual host specified. Be sure to check
  96. your broker logs (for RabbitMQ that is ``/var/log/rabbitmq/rabbit.log`` on
  97. most systems), it usually contains a message describing the reason.
  98. Why won't celeryd run on FreeBSD?
  99. ---------------------------------
  100. **Answer:** multiprocessing.Pool requires a working POSIX semaphore
  101. implementation which isn't enabled in FreeBSD by default. You have to enable
  102. POSIX semaphores in the kernel and manually recompile multiprocessing.
  103. Luckily, Viktor Petersson has written a tutorial to get you started with
  104. Celery on FreeBSD here:
  105. http://www.playingwithwire.com/2009/10/how-to-get-celeryd-to-work-on-freebsd/
  106. I'm having ``IntegrityError: Duplicate Key`` errors. Why?
  107. ---------------------------------------------------------
  108. **Answer:** See `MySQL is throwing deadlock errors, what can I do?`_.
  109. Thanks to howsthedotcom.
  110. Why aren't my tasks processed?
  111. ------------------------------
  112. **Answer:** With RabbitMQ you can see how many consumers are currently
  113. receiving tasks by running the following command::
  114. $ rabbitmqctl list_queues -p <myvhost> name messages consumers
  115. Listing queues ...
  116. celery 2891 2
  117. This shows that there's 2891 messages waiting to be processed in the task
  118. queue, and there are two consumers processing them.
  119. One reason that the queue is never emptied could be that you have a stale
  120. celery process taking the messages hostage. This could happen if celeryd
  121. wasn't properly shut down.
  122. When a message is recieved by a worker the broker waits for it to be
  123. acknowledged before marking the message as processed. The broker will not
  124. re-send that message to another consumer until the consumer is shut down
  125. properly.
  126. If you hit this problem you have to kill all workers manually and restart
  127. them::
  128. ps auxww | grep celeryd | awk '{print $2}' | xargs kill
  129. You might have to wait a while until all workers have finished the work they're
  130. doing. If it's still hanging after a long time you can kill them by force
  131. with::
  132. ps auxww | grep celeryd | awk '{print $2}' | xargs kill -9
  133. Why won't my Task run?
  134. ----------------------
  135. **Answer:** There might be syntax errors preventing the tasks module being imported.
  136. You can find out if celery is able to run the task by executing the
  137. task manually:
  138. >>> from myapp.tasks import MyPeriodicTask
  139. >>> MyPeriodicTask.delay()
  140. Watch celeryds logfile to see if it's able to find the task, or if some
  141. other error is happening.
  142. Why won't my Periodic Task run?
  143. -------------------------------
  144. **Answer:** See `Why won't my Task run?`_.
  145. How do I discard all waiting tasks?
  146. ------------------------------------
  147. **Answer:** Use ``celery.task.discard_all()``, like this:
  148. >>> from celery.task import discard_all
  149. >>> discard_all()
  150. 1753
  151. The number ``1753`` is the number of messages deleted.
  152. You can also start celeryd with the ``--discard`` argument which will
  153. accomplish the same thing.
  154. I've discarded messages, but there are still messages left in the queue?
  155. ------------------------------------------------------------------------
  156. **Answer:** Tasks are acknowledged (removed from the queue) as soon
  157. as they are actually executed. After the worker has received a task, it will
  158. take some time until it is actually executed, especially if there are a lot
  159. of tasks already waiting for execution. Messages that are not acknowledged are
  160. hold on to by the worker until it closes the connection to the broker (AMQP
  161. server). When that connection is closed (e.g because the worker was stopped)
  162. the tasks will be re-sent by the broker to the next available worker (or the
  163. same worker when it has been restarted), so to properly purge the queue of
  164. waiting tasks you have to stop all the workers, and then discard the tasks
  165. using ``discard_all``.
  166. Windows: The ``-B`` / ``--beat`` option to celeryd doesn't work?
  167. ----------------------------------------------------------------
  168. **Answer**: That's right. Run ``celerybeat`` and ``celeryd`` as separate
  169. services instead.
  170. Tasks
  171. =====
  172. How can I reuse the same connection when applying tasks?
  173. --------------------------------------------------------
  174. **Answer**: See :doc:`userguide/executing`.
  175. Can I execute a task by name?
  176. -----------------------------
  177. **Answer**: Yes. Use :func:`celery.execute.send_task`.
  178. You can also execute a task by name from any language
  179. that has an AMQP client.
  180. >>> from celery.execute import send_task
  181. >>> send_task("tasks.add", args=[2, 2], kwargs={})
  182. <AsyncResult: 373550e8-b9a0-4666-bc61-ace01fa4f91d>
  183. Results
  184. =======
  185. How dow I get the result of a task if I have the ID that points there?
  186. ----------------------------------------------------------------------
  187. **Answer**: Use ``Task.AsyncResult``::
  188. >>> result = MyTask.AsyncResult(task_id)
  189. >>> result.get()
  190. This will give you a :class:`celery.result.BaseAsyncResult` instance
  191. using the tasks current result backend.
  192. If you need to specify a custom result backend you should use
  193. :class:`celery.result.BaseAsyncResult` directly::
  194. >>> from celery.result import BaseAsyncResult
  195. >>> result = BaseAsyncResult(task_id, backend=...)
  196. >>> result.get()
  197. Brokers
  198. =======
  199. Why is RabbitMQ crashing?
  200. -------------------------
  201. RabbitMQ will crash if it runs out of memory. This will be fixed in a
  202. future release of RabbitMQ. please refer to the RabbitMQ FAQ:
  203. http://www.rabbitmq.com/faq.html#node-runs-out-of-memory
  204. Some common Celery misconfigurations can crash RabbitMQ:
  205. * Events.
  206. Running ``celeryd`` with the ``-E``/``--events`` option will send messages
  207. for events happening inside of the worker. If these event messages
  208. are not consumed, you will eventually run out of memory.
  209. Events should only be enabled if you have an active monitor consuming them.
  210. * AMQP backend results.
  211. When running with the AMQP result backend, every task result will be sent
  212. as a message. If you don't collect these results, they will build up and
  213. RabbitMQ will eventually run out of memory.
  214. If you don't use the results for a task, make sure you set the
  215. ``ignore_result`` option:
  216. .. code-block python
  217. @task(ignore_result=True)
  218. def mytask():
  219. ...
  220. class MyTask(Task):
  221. ignore_result = True
  222. Results can also be disabled globally using the ``CELERY_IGNORE_RESULT``
  223. setting.
  224. Can I use celery with ActiveMQ/STOMP?
  225. -------------------------------------
  226. **Answer**: Yes, but this is somewhat experimental for now.
  227. It is working ok in a test configuration, but it has not
  228. been tested in production like RabbitMQ has. If you have any problems with
  229. using STOMP and celery, please report the bugs to the issue tracker:
  230. http://github.com/ask/celery/issues/
  231. First you have to use the ``master`` branch of ``celery``::
  232. $ git clone git://github.com/ask/celery.git
  233. $ cd celery
  234. $ sudo python setup.py install
  235. $ cd ..
  236. Then you need to install the ``stompbackend`` branch of ``carrot``::
  237. $ git clone git://github.com/ask/carrot.git
  238. $ cd carrot
  239. $ git checkout stompbackend
  240. $ sudo python setup.py install
  241. $ cd ..
  242. And my fork of ``python-stomp`` which adds non-blocking support::
  243. $ hg clone http://bitbucket.org/asksol/python-stomp/
  244. $ cd python-stomp
  245. $ sudo python setup.py install
  246. $ cd ..
  247. In this example we will use a queue called ``celery`` which we created in
  248. the ActiveMQ web admin interface.
  249. **Note**: For ActiveMQ the queue name has to have ``"/queue/"`` prepended to
  250. it. i.e. the queue ``celery`` becomes ``/queue/celery``.
  251. Since a STOMP queue is a single named entity and it doesn't have the
  252. routing capabilities of AMQP you need to set both the ``queue``, and
  253. ``exchange`` settings to your queue name. This is a minor inconvenience since
  254. carrot needs to maintain the same interface for both AMQP and STOMP (obviously
  255. the one with the most capabilities won).
  256. Use the following specific settings in your ``settings.py``:
  257. .. code-block:: python
  258. # Makes python-stomp the default backend for carrot.
  259. CARROT_BACKEND = "stomp"
  260. # STOMP hostname and port settings.
  261. BROKER_HOST = "localhost"
  262. BROKER_PORT = 61613
  263. # The queue name to use (both queue and exchange must be set to the
  264. # same queue name when using STOMP)
  265. CELERY_DEFAULT_QUEUE = "/queue/celery"
  266. CELERY_DEFAULT_EXCHANGE = "/queue/celery"
  267. CELERY_QUEUES = {
  268. "/queue/celery": {"exchange": "/queue/celery"}
  269. }
  270. Now you can go on reading the tutorial in the README, ignoring any AMQP
  271. specific options.
  272. What features are not supported when using STOMP?
  273. --------------------------------------------------
  274. This is a (possible incomplete) list of features not available when
  275. using the STOMP backend:
  276. * routing keys
  277. * exchange types (direct, topic, headers, etc)
  278. * immediate
  279. * mandatory
  280. Features
  281. ========
  282. How can I run a task once another task has finished?
  283. ----------------------------------------------------
  284. **Answer**: You can safely launch a task inside a task.
  285. Also, a common pattern is to use callback tasks:
  286. .. code-block:: python
  287. @task()
  288. def add(x, y, callback=None):
  289. result = x + y
  290. if callback:
  291. callback.delay(result)
  292. return result
  293. @task(ignore_result=True)
  294. def log_result(result, **kwargs):
  295. logger = log_result.get_logger(**kwargs)
  296. logger.info("log_result got: %s" % (result, ))
  297. >>> add.delay(2, 2, callback=log_result)
  298. Can I cancel the execution of a task?
  299. -------------------------------------
  300. **Answer**: Yes. Use ``result.revoke``::
  301. >>> result = add.apply_async(args=[2, 2], countdown=120)
  302. >>> result.revoke()
  303. or if you only have the task id::
  304. >>> from celery.task.control import revoke
  305. >>> revoke(task_id)
  306. Why aren't my remote control commands received by all workers?
  307. --------------------------------------------------------------
  308. **Answer**: To receive broadcast remote control commands, every ``celeryd``
  309. uses its hostname to create a unique queue name to listen to,
  310. so if you have more than one worker with the same hostname, the
  311. control commands will be recieved in round-robin between them.
  312. To work around this you can explicitly set the hostname for every worker
  313. using the ``--hostname`` argument to ``celeryd``::
  314. $ celeryd --hostname=$(hostname).1
  315. $ celeryd --hostname=$(hostname).2
  316. etc, etc.
  317. Can I send some tasks to only some servers?
  318. --------------------------------------------
  319. **Answer:** Yes. You can route tasks to an arbitrary server using AMQP,
  320. and a worker can bind to as many queues as it wants.
  321. Say you have two servers, ``x``, and ``y`` that handles regular tasks,
  322. and one server ``z``, that only handles feed related tasks, you can use this
  323. configuration:
  324. * Servers ``x`` and ``y``: settings.py:
  325. .. code-block:: python
  326. CELERY_DEFAULT_QUEUE = "regular_tasks"
  327. CELERY_QUEUES = {
  328. "regular_tasks": {
  329. "binding_key": "task.#",
  330. },
  331. }
  332. CELERY_DEFAULT_EXCHANGE = "tasks"
  333. CELERY_DEFAULT_EXCHANGE_TYPE = "topic"
  334. CELERY_DEFAULT_ROUTING_KEY = "task.regular"
  335. * Server ``z``: settings.py:
  336. .. code-block:: python
  337. CELERY_DEFAULT_QUEUE = "feed_tasks"
  338. CELERY_QUEUES = {
  339. "feed_tasks": {
  340. "binding_key": "feed.#",
  341. },
  342. }
  343. CELERY_DEFAULT_EXCHANGE = "tasks"
  344. CELERY_DEFAULT_ROUTING_KEY = "task.regular"
  345. CELERY_DEFAULT_EXCHANGE_TYPE = "topic"
  346. ``CELERY_QUEUES`` is a map of queue names and their exchange/type/binding_key,
  347. if you don't set exchange or exchange type, they will be taken from the
  348. ``CELERY_DEFAULT_EXCHANGE``/``CELERY_DEFAULT_EXCHANGE_TYPE`` settings.
  349. Now to make a Task run on the ``z`` server you need to set its
  350. ``routing_key`` attribute so it starts with the words ``"task.feed."``:
  351. .. code-block:: python
  352. from feedaggregator.models import Feed
  353. from celery.decorators import task
  354. @task(routing_key="feed.importer")
  355. def import_feed(feed_url):
  356. Feed.objects.import_feed(feed_url)
  357. or if subclassing the ``Task`` class directly:
  358. .. code-block:: python
  359. class FeedImportTask(Task):
  360. routing_key = "feed.importer"
  361. def run(self, feed_url):
  362. Feed.objects.import_feed(feed_url)
  363. You can also override this using the ``routing_key`` argument to
  364. :func:`celery.task.apply_async`:
  365. >>> from myapp.tasks import RefreshFeedTask
  366. >>> RefreshFeedTask.apply_async(args=["http://cnn.com/rss"],
  367. ... routing_key="feed.importer")
  368. If you want, you can even have your feed processing worker handle regular
  369. tasks as well, maybe in times when there's a lot of work to do.
  370. Just add a new queue to server ``z``'s ``CELERY_QUEUES``:
  371. .. code-block:: python
  372. CELERY_QUEUES = {
  373. "feed_tasks": {
  374. "binding_key": "feed.#",
  375. },
  376. "regular_tasks": {
  377. "binding_key": "task.#",
  378. },
  379. }
  380. Since the default exchange is ``tasks``, they will both use the same
  381. exchange.
  382. If you have another queue but on another exchange you want to add,
  383. just specify a custom exchange and exchange type:
  384. .. code-block:: python
  385. CELERY_QUEUES = {
  386. "feed_tasks": {
  387. "binding_key": "feed.#",
  388. },
  389. "regular_tasks": {
  390. "binding_key": "task.#",
  391. }
  392. "image_tasks": {
  393. "binding_key": "image.compress",
  394. "exchange": "mediatasks",
  395. "exchange_type": "direct",
  396. },
  397. }
  398. If you're confused about these terms, you should read up on AMQP and RabbitMQ.
  399. `Rabbits and Warrens`_ is an excellent blog post describing queues and
  400. exchanges. There's also AMQP in 10 minutes*: `Flexible Routing Model`_,
  401. and `Standard Exchange Types`_. For users of RabbitMQ the `RabbitMQ FAQ`_
  402. could also be useful as a source of information.
  403. .. _`Rabbits and Warrens`: http://blogs.digitar.com/jjww/2009/01/rabbits-and-warrens/
  404. .. _`Flexible Routing Model`: http://bit.ly/95XFO1
  405. .. _`Standard Exchange Types`: http://bit.ly/EEWca
  406. .. _`RabbitMQ FAQ`: http://www.rabbitmq.com/faq.html
  407. Can I change the interval of a periodic task at runtime?
  408. --------------------------------------------------------
  409. **Answer**: Yes. You can override ``PeriodicTask.is_due`` or turn
  410. ``PeriodicTask.run_every`` into a property:
  411. .. code-block:: python
  412. class MyPeriodic(PeriodicTask):
  413. def run(self):
  414. # ...
  415. @property
  416. def run_every(self):
  417. return get_interval_from_database(...)
  418. Does celery support task priorities?
  419. ------------------------------------
  420. **Answer**: No. In theory, yes, as AMQP supports priorities. However
  421. RabbitMQ doesn't implement them yet.
  422. The usual way to prioritize work in celery, is to route high priority tasks
  423. to different servers. In the real world this may actually work better than per message
  424. priorities. You can use this in combination with rate limiting to achieve a
  425. highly performant system.
  426. Should I use retry or acks_late?
  427. --------------------------------
  428. **Answer**: Depends. It's not necessarily one or the other, you may want
  429. to use both.
  430. ``Task.retry`` is used to retry tasks, notably for expected errors that
  431. is catchable with the ``try:`` block. The AMQP transaction is not used
  432. for these errors: **if the task raises an exception it is still acked!**.
  433. The ``acks_late`` setting would be used when you need the task to be
  434. executed again if the worker (for some reason) crashes mid-execution.
  435. It's important to note that the worker is not known to crash, and if
  436. it does it is usually an unrecoverable error that requires human
  437. intervention (bug in the worker, or task code).
  438. In an ideal world you could safely retry any task that has failed, but
  439. this is rarely the case. Imagine the following task:
  440. .. code-block:: python
  441. @task()
  442. def process_upload(filename, tmpfile):
  443. # Increment a file count stored in a database
  444. increment_file_counter()
  445. add_file_metadata_to_db(filename, tmpfile)
  446. copy_file_to_destination(filename, tmpfile)
  447. If this crashed in the middle of copying the file to its destination
  448. the world would contain incomplete state. This is not a critical
  449. scenario of course, but you can probably imagine something far more
  450. sinister. So for ease of programming we have less reliability;
  451. It's a good default, users who require it and know what they
  452. are doing can still enable acks_late (and in the future hopefully
  453. use manual acknowledgement)
  454. In addition ``Task.retry`` has features not available in AMQP
  455. transactions: delay between retries, max retries, etc.
  456. So use retry for Python errors, and if your task is reentrant
  457. combine that with ``acks_late`` if that level of reliability
  458. is required.
  459. Can I schedule tasks to execute at a specific time?
  460. ---------------------------------------------------
  461. .. module:: celery.task.base
  462. **Answer**: Yes. You can use the ``eta`` argument of :meth:`Task.apply_async`.
  463. Or to schedule a periodic task at a specific time, use the
  464. :class:`celery.task.schedules.crontab` schedule behavior:
  465. .. code-block:: python
  466. from celery.task.schedules import crontab
  467. from celery.decorators import periodic_task
  468. @periodic_task(run_every=crontab(hours=7, minute=30, day_of_week="mon"))
  469. def every_monday_morning():
  470. print("This is run every monday morning at 7:30")
  471. How do I shut down ``celeryd`` safely?
  472. --------------------------------------
  473. **Answer**: Use the ``TERM`` signal, and celery will finish all currently
  474. executing jobs and shut down as soon as possible. No tasks should be lost.
  475. You should never stop ``celeryd`` with the ``KILL`` signal (``-9``),
  476. unless you've tried ``TERM`` a few times and waited a few minutes to let it
  477. get a chance to shut down. As if you do tasks may be terminated mid-execution,
  478. and they will not be re-run unless you have the ``acks_late`` option set.
  479. (``Task.acks_late`` / ``CELERY_ACKS_LATE``).
  480. How do I run celeryd in the background on [platform]?
  481. -----------------------------------------------------
  482. **Answer**: Please see :doc:`cookbook/daemonizing`.