FAQ 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. ============================
  2. Frequently Asked Questions
  3. ============================
  4. Troubleshooting
  5. ===============
  6. MySQL is throwing deadlock errors, what can I do?
  7. -------------------------------------------------
  8. **Answer:** MySQL has default isolation level set to ``REPEATABLE-READ``,
  9. if you don't really need that, set it to ``READ-COMMITTED``.
  10. You can do that by adding the following to your ``my.cnf``::
  11. [mysqld]
  12. transaction-isolation = READ-COMMITTED
  13. For more information about InnoDBs transaction model see `MySQL - The InnoDB
  14. Transaction Model and Locking`_ in the MySQL user manual.
  15. (Thanks to Honza Kral and Anton Tsigularov for this solution)
  16. .. _`MySQL - The InnoDB Transaction Model and Locking`: http://dev.mysql.com/doc/refman/5.1/en/innodb-transaction-model.html
  17. celeryd is not doing anything, just hanging
  18. --------------------------------------------
  19. **Answer:** See `MySQL is throwing deadlock errors, what can I do?`_.
  20. or `Why is Task.delay/apply\* just hanging?`.
  21. Why is Task.delay/apply\*/celeryd just hanging?
  22. -----------------------------------------------
  23. **Answer:** There is a bug in some AMQP clients that will make it hang if
  24. it's not able to authenticate the current user, the password doesn't match or
  25. the user does not have access to the virtual host specified. Be sure to check
  26. your broker logs (for RabbitMQ that is ``/var/log/rabbitmq/rabbit.log`` on
  27. most systems), it usually contains a message describing the reason.
  28. Why won't celeryd run on FreeBSD?
  29. ---------------------------------
  30. **Answer:** multiprocessing.Pool requires a working POSIX semaphore
  31. implementation which isn't enabled in FreeBSD by default. You have to enable
  32. POSIX semaphores in the kernel and manually recompile multiprocessing.
  33. Luckily, Viktor Petersson has written a tutorial to get you started with
  34. Celery on FreeBSD here:
  35. http://www.playingwithwire.com/2009/10/how-to-get-celeryd-to-work-on-freebsd/
  36. I'm having ``IntegrityError: Duplicate Key`` errors. Why?
  37. ----------------------------------------------------------
  38. **Answer:** See `MySQL is throwing deadlock errors, what can I do?`_.
  39. Thanks to howsthedotcom.
  40. Why isn't my tasks processed?
  41. -----------------------------
  42. **Answer:** With RabbitMQ you can see how many consumers are currently
  43. receiving tasks by running the following command::
  44. $ rabbitmqctl list_queues -p <myvhost> name messages consumers
  45. Listing queues ...
  46. celery 2891 2
  47. This shows that there's 2891 messages waiting to be processed in the task
  48. queue, and there are two consumers processing them.
  49. One reason that the queue is never emptied could be that you have a stale
  50. celery process taking the messages hostage. This could happen if celeryd
  51. wasn't properly shut down.
  52. When a message is recieved by a worker the broker waits for it to be
  53. acknowledged before marking the message as processed. The broker will not
  54. re-send that message to another consumer until the consumer is shutdown
  55. properly.
  56. If you hit this problem you have to kill all workers manually and restart
  57. them::
  58. ps auxww | grep celeryd | awk '{print $2}' | xargs kill
  59. You might have to wait a while until all workers has finished the work they're
  60. doing, if it's still hanging after a long time you can kill them by force
  61. with::
  62. ps auxww | grep celeryd | awk '{print $2}' | xargs kill -9
  63. Why won't my Task run?
  64. ----------------------
  65. **Answer:** Did you register the task in the applications ``tasks.py`` module?
  66. (or in some other module Django loads by default, like ``models.py``?).
  67. Also there might be syntax errors preventing the tasks module being imported.
  68. You can find out if the celery daemon is able to run the task by executing the
  69. task manually:
  70. >>> from myapp.tasks import MyPeriodicTask
  71. >>> MyPeriodicTask.delay()
  72. Watch celery daemons logfile (or output if not running as a daemon), to see
  73. if it's able to find the task, or if some other error is happening.
  74. Why won't my Periodic Task run?
  75. -------------------------------
  76. **Answer:** See `Why won't my Task run?`_.
  77. How do I discard all waiting tasks?
  78. ------------------------------------
  79. **Answer:** Use ``celery.task.discard_all()``, like this:
  80. >>> from celery.task import discard_all
  81. >>> discard_all()
  82. 1753
  83. The number ``1753`` is the number of messages deleted.
  84. You can also start celeryd with the ``--discard`` argument which will
  85. accomplish the same thing.
  86. I've discarded messages, but there are still messages left in the queue?
  87. ------------------------------------------------------------------------
  88. **Answer:** Tasks are acknowledged (removed from the queue) as soon
  89. as they are actually executed. After the worker has received a task, it will
  90. take some time until it is actually executed, especially if there are a lot
  91. of tasks already waiting for execution. Messages that are not acknowledged are
  92. hold on to by the worker until it closes the connection to the broker (AMQP
  93. server). When that connection is closed (e.g because the worker was stopped)
  94. the tasks will be re-sent by the broker to the next available worker (or the
  95. same worker when it has been restarted), so to properly purge the queue of
  96. waiting tasks you have to stop all the workers, and then discard the tasks
  97. using ``discard_all``.
  98. Brokers
  99. =======
  100. Can I use celery with ActiveMQ/STOMP?
  101. -------------------------------------
  102. **Answer**: Yes. But this is somewhat experimental for now.
  103. It is certainly working ok for me in a test configuration, but it has not
  104. been tested in production like RabbitMQ. If you have any problems with
  105. using STOMP and celery, please report the bugs to the issue tracker:
  106. http://github.com/ask/celery/issues/
  107. First you have to use the ``master`` branch of ``celery``::
  108. $ git clone git://github.com/ask/celery.git
  109. $ cd celery
  110. $ sudo python setup.py install
  111. $ cd ..
  112. Then you need to install the ``stompbackend`` branch of ``carrot``::
  113. $ git clone git://github.com/ask/carrot.git
  114. $ cd carrot
  115. $ git checkout stompbackend
  116. $ sudo python setup.py install
  117. $ cd ..
  118. And my fork of ``python-stomp`` which adds non-blocking support::
  119. $ hg clone http://bitbucket.org/asksol/python-stomp/
  120. $ cd python-stomp
  121. $ sudo python setup.py install
  122. $ cd ..
  123. In this example we will use a queue called ``celery`` which we created in
  124. the ActiveMQ web admin interface.
  125. **Note**: For ActiveMQ the queue name has to have ``"/queue/"`` prepended to
  126. it. i.e. the queue ``celery`` becomes ``/queue/celery``.
  127. Since a STOMP queue is a single named entity and it doesn't have the
  128. routing capabilities of AMQP you need to set both the ``queue``, and
  129. ``exchange`` settings to your queue name. This is a minor inconvenience since
  130. carrot needs to maintain the same interface for both AMQP and STOMP (obviously
  131. the one with the most capabilities won).
  132. Use the following specific settings in your ``settings.py``:
  133. .. code-block:: python
  134. # Makes python-stomp the default backend for carrot.
  135. CARROT_BACKEND = "stomp"
  136. # STOMP hostname and port settings.
  137. AMQP_SERVER = "localhost"
  138. AMQP_PORT = 61613
  139. # The queue name to use (both queue and exchange must be set to the
  140. # same queue name when using STOMP)
  141. CELERY_AMQP_CONSUMER_QUEUE = "/queue/celery"
  142. CELERY_AMQP_EXCHANGE = "/queue/celery"
  143. Now you can go on reading the tutorial in the README, ignoring any AMQP
  144. specific options.
  145. Which features are not supported when using STOMP?
  146. --------------------------------------------------
  147. This is a (possible incomplete) list of features not available when
  148. using the STOMP backend:
  149. * routing keys
  150. * exchange types (direct, topic, headers, etc)
  151. * immediate
  152. * mandatory
  153. Features
  154. ========
  155. Can I send some tasks to only some servers?
  156. --------------------------------------------
  157. **Answer:** As of now there is only one use-case that works like this,
  158. and that is tasks of type ``A`` can be sent to servers ``x`` and ``y``,
  159. while tasks of type ``B`` can be sent to server ``z``. One server can't
  160. handle more than one routing_key, but this is coming in a later release.
  161. Say you have two servers, ``x``, and ``y`` that handles regular tasks,
  162. and one server ``z``, that only handles feed related tasks, you can use this
  163. configuration:
  164. * Servers ``x`` and ``y``: settings.py:
  165. .. code-block:: python
  166. AMQP_SERVER = "rabbit"
  167. AMQP_PORT = 5678
  168. AMQP_USER = "myapp"
  169. AMQP_PASSWORD = "secret"
  170. AMQP_VHOST = "myapp"
  171. CELERY_AMQP_CONSUMER_QUEUE = "regular_tasks"
  172. CELERY_AMQP_EXCHANGE = "tasks"
  173. CELERY_AMQP_PUBLISHER_ROUTING_KEY = "task.regular"
  174. CELERY_AMQP_CONSUMER_ROUTING_KEY = "task.#"
  175. CELERY_AMQP_EXCHANGE_TYPE = "topic"
  176. * Server ``z``: settings.py:
  177. .. code-block:: python
  178. AMQP_SERVER = "rabbit"
  179. AMQP_PORT = 5678
  180. AMQP_USER = "myapp"
  181. AMQP_PASSWORD = "secret"
  182. AMQP_VHOST = "myapp"
  183. CELERY_AMQP_EXCHANGE = "tasks"
  184. CELERY_AMQP_PUBLISHER_ROUTING_KEY = "task.regular"
  185. CELERY_AMQP_EXCHANGE_TYPE = "topic"
  186. # This is the settings different for this server:
  187. CELERY_AMQP_CONSUMER_QUEUE = "feed_tasks"
  188. CELERY_AMQP_CONSUMER_ROUTING_KEY = "feed.#"
  189. Now to make a Task run on the ``z`` server you need to set its
  190. ``routing_key`` attribute so it starts with the words ``"task.feed."``:
  191. .. code-block:: python
  192. from feedaggregator.models import Feed
  193. from celery.task import Task
  194. class FeedImportTask(Task):
  195. routing_key = "feed.importer"
  196. def run(self, feed_url):
  197. # something importing the feed
  198. Feed.objects.import_feed(feed_url)
  199. You can also override this using the ``routing_key`` argument to
  200. :func:`celery.task.apply_async`:
  201. >>> from celery.task import apply_async
  202. >>> from myapp.tasks import RefreshFeedTask
  203. >>> apply_async(RefreshFeedTask, args=["http://cnn.com/rss"],
  204. ... routing_key="feed.importer")
  205. Can I use celery without Django?
  206. --------------------------------
  207. **Answer:** Yes.
  208. Celery uses something called loaders to read/setup configuration, import
  209. modules that registers tasks and to decide what happens when a task is
  210. executed. Currently there are two loaders, the default loader and the Django
  211. loader. If you want to use celery without a Django project, you either have to
  212. use the default loader, or write a loader of your own.
  213. The rest of this answer describes how to use the default loader.
  214. First of all, installation. You need to get the development version of
  215. celery from github::
  216. $ git clone git://github.com/ask/celery.git
  217. $ cd celery
  218. # python setup.py install # as root
  219. While it is possible to use celery from outside of Django, we still need
  220. Django itself to run, this is to use the ORM and cache-framework, etc.
  221. Duplicating these features would be time consuming and mostly pointless, so
  222. we decided that having a dependency on Django itself was a good thing.
  223. Install Django using your favorite install tool, ``easy_install``, ``pip``, or
  224. whatever::
  225. # easy_install django # as root
  226. You need a configuration file named ``celeryconfig.py``, either in the
  227. directory you run ``celeryd`` in, or in a Python library path where it is
  228. able to find it. The configuration file can contain any of the settings
  229. described in :mod:`celery.conf`, and in additional if you're using the
  230. database backend you have to configure the database. Here is an example
  231. configuration using the database backend with MySQL:
  232. .. code-block:: python
  233. # Broker configuration
  234. AMQP_SERVER = "localhost"
  235. AMQP_PORT = "5672"
  236. AMQP_VHOST = "celery"
  237. AMQP_USER = "celery"
  238. AMQP_PASSWORD = "celerysecret"
  239. CARROT_BACKEND="amqp"
  240. # Using the database backend.
  241. CELERY_BACKEND = "database"
  242. DATABASE_ENGINE = "mysql" # see Django docs for a description of these.
  243. DATABASE_NAME = "mydb"
  244. DATABASE_HOST = "mydb.example.org"
  245. DATABASE_USER = "myuser"
  246. DATABASE_PASSWORD = "mysecret"
  247. # Number of processes that processes tasks simultaneously.
  248. CELERYD_CONCURRENCY = 8
  249. # Modules to import when celeryd starts.
  250. # This must import every module where you register tasks so celeryd
  251. # is able to find and run them.
  252. CELERY_IMPORTS = ("mytaskmodule1", "mytaskmodule2")
  253. Now with this configuration file in the current directory you have to
  254. run ``celeryinit`` to create the database tables::
  255. $ celeryinit
  256. Then you should be able to successfully run ``celeryd``::
  257. $ celeryd --loglevel=INFO
  258. and send a task from a python shell (note that it must be able to import
  259. ``celeryconfig.py``):
  260. >>> from celery.task.builtins import PingTask
  261. >>> result = PingTask.apply_async()
  262. >>> result.get()
  263. 'pong'
  264. The celery test-suite is failing
  265. --------------------------------
  266. **Answer**: You're running tests from your own Django applicaiton, and celerys
  267. tests are failing and celerys tests are failing in that context?
  268. If so, read on for a trick, if not please report the test failure to our issue
  269. tracker at GitHub.
  270. http://github.com/ask/celery/issues/
  271. That Django is running tests for all applications in ``INSTALLED_APPS``
  272. is a pet peeve of mine. You should use a test runner that either
  273. 1) Explicitly lists the apps you want to run tests for, or
  274. 2) make a test runner that skips tests for apps you don't want to run.
  275. For example this test runner that celery is using:
  276. http://bit.ly/NVKep
  277. To use this add the following to your settings.py:
  278. .. code-block:: python
  279. TEST_RUNNER = "celery.tests.runners.run_tests"
  280. TEST_APPS = (
  281. "app1",
  282. "app2",
  283. "app3",
  284. "app4",
  285. )
  286. If you just want to skip celery you could use:
  287. .. code-block:: python
  288. INSTALLED_APPS = (.....)
  289. TEST_RUNNER = "celery.tests.runners.run_tests"
  290. TEST_APPS = filter(lambda k: k != "celery", INSTALLED_APPS)