FAQ 12 KB

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