FAQ 12 KB

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