FAQ 10 KB

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