first-steps-with-celery.rst 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. .. _tut-celery:
  2. ========================
  3. First steps with Celery
  4. ========================
  5. .. contents::
  6. :local:
  7. .. _celerytut-broker:
  8. Choosing your Broker
  9. ====================
  10. Before you can use Celery you need to choose, install and run a broker.
  11. The broker is the service responsible for receiving and delivering task
  12. messages.
  13. There are several choices available, including:
  14. * :ref:`broker-rabbitmq`
  15. `RabbitMQ`_ is feature-complete, safe and durable. If not losing tasks
  16. is important to you, then this is your best option.
  17. * :ref:`broker-redis`
  18. `Redis`_ is also feature-complete, but power failures or abrupt termination
  19. may result in data loss.
  20. * :ref:`broker-sqlalchemy`
  21. * :ref:`broker-django`
  22. Using a database as a message queue is not recommended, but can be sufficient
  23. for very small installations. Celery can use the SQLAlchemy and Django ORM.
  24. * and more.
  25. In addition to the above, there are several other transport implementations
  26. to choose from, including :ref:`broker-couchdb`, :ref:`broker-beanstalk`,
  27. :ref:`broker-mongodb`, and SQS. There is a `Transport Comparison`_
  28. in the Kombu documentation.
  29. .. _`RabbitMQ`: http://www.rabbitmq.com/
  30. .. _`Redis`: http://redis.io/
  31. .. _`Transport Comparison`: http://kombu.rtfd.org/transport-comparison
  32. .. _celerytut-simple-tasks:
  33. Creating a simple task
  34. ======================
  35. In this tutorial we are creating a simple task that adds two
  36. numbers. Tasks are defined in normal Python modules.
  37. By convention we will call our module :file:`tasks.py`, and it looks
  38. like this:
  39. :file: `tasks.py`
  40. .. code-block:: python
  41. from celery.task import task
  42. @task
  43. def add(x, y):
  44. return x + y
  45. Behind the scenes the `@task` decorator actually creates a class that
  46. inherits from :class:`~celery.task.base.Task`. The best practice is to
  47. only create custom task classes when you want to change generic behavior,
  48. and use the decorator to define tasks.
  49. .. seealso::
  50. The full documentation on how to create tasks and task classes is in the
  51. :doc:`../userguide/tasks` part of the user guide.
  52. .. _celerytut-conf:
  53. Configuration
  54. =============
  55. Celery is configured by using a configuration module. By default
  56. this module is called :file:`celeryconfig.py`.
  57. The configuration module must either be in the current directory
  58. or on the Python path, so that it can be imported.
  59. You can also set a custom name for the configuration module by using
  60. the :envvar:`CELERY_CONFIG_MODULE` environment variable.
  61. Let's create our :file:`celeryconfig.py`.
  62. 1. Configure how we communicate with the broker (RabbitMQ in this example)::
  63. BROKER_URL = "amqp://guest:guest@localhost:5672//"
  64. 2. Define the backend used to store task metadata and return values::
  65. CELERY_RESULT_BACKEND = "amqp"
  66. The AMQP backend is non-persistent by default, and you can only
  67. fetch the result of a task once (as it's sent as a message).
  68. For list of backends available and related options see
  69. :ref:`conf-result-backend`.
  70. 3. Finally we list the modules the worker should import. This includes
  71. the modules containing your tasks.
  72. We only have a single task module, :file:`tasks.py`, which we added earlier::
  73. CELERY_IMPORTS = ("tasks", )
  74. That's it.
  75. There are more options available, like how many processes you want to
  76. use to process work in parallel (the :setting:`CELERY_CONCURRENCY` setting),
  77. and we could use a persistent result store backend, but for now, this should
  78. do. For all of the options available, see :ref:`configuration`.
  79. .. note::
  80. You can also specify modules to import using the :option:`-I` option to
  81. :mod:`~celery.bin.celeryd`::
  82. $ celeryd -l info -I tasks,handlers
  83. This can be a single, or a comma separated list of task modules to import
  84. when :program:`celeryd` starts.
  85. .. _celerytut-running-celeryd:
  86. Running the celery worker server
  87. ================================
  88. To test we will run the worker server in the foreground, so we can
  89. see what's going on in the terminal::
  90. $ celeryd --loglevel=INFO
  91. In production you will probably want to run the worker in the
  92. background as a daemon. To do this you need to use the tools provided
  93. by your platform, or something like `supervisord`_ (see :ref:`daemonizing`
  94. for more information).
  95. For a complete listing of the command line options available, do::
  96. $ celeryd --help
  97. .. _`supervisord`: http://supervisord.org
  98. .. _celerytut-executing-task:
  99. Executing the task
  100. ==================
  101. Whenever we want to execute our task, we use the
  102. :meth:`~celery.task.base.Task.delay` method of the task class.
  103. This is a handy shortcut to the :meth:`~celery.task.base.Task.apply_async`
  104. method which gives greater control of the task execution (see
  105. :ref:`guide-executing`).
  106. >>> from tasks import add
  107. >>> add.delay(4, 4)
  108. <AsyncResult: 889143a6-39a2-4e52-837b-d80d33efb22d>
  109. At this point, the task has been sent to the message broker. The message
  110. broker will hold on to the task until a worker server has consumed and
  111. executed it.
  112. Right now we have to check the worker log files to know what happened
  113. with the task. Applying a task returns an
  114. :class:`~celery.result.AsyncResult`, if you have configured a result store
  115. the :class:`~celery.result.AsyncResult` enables you to check the state of
  116. the task, wait for the task to finish, get its return value
  117. or exception/traceback if the task failed, and more.
  118. Keeping Results
  119. ---------------
  120. If you want to keep track of the tasks state, Celery needs to store or send
  121. the states somewhere. There are several
  122. built-in backends to choose from: SQLAlchemy/Django ORM, Memcached, Redis,
  123. AMQP, MongoDB, Tokyo Tyrant and Redis -- or you can define your own.
  124. For this example we will use the `amqp` result backend, which sends states
  125. as messages. The backend is configured via the ``CELERY_RESULT_BACKEND``
  126. option, in addition individual result backends may have additional settings
  127. you can configure::
  128. CELERY_RESULT_BACKEND = "amqp"
  129. #: We want the results to expire in 5 minutes, note that this requires
  130. #: RabbitMQ version 2.1.1 or higher, so please comment out if you have
  131. #: an earlier version.
  132. CELERY_TASK_RESULT_EXPIRES = 300
  133. To read more about result backends please see :ref:`task-result-backends`.
  134. Now with the result backend configured, let's execute the task again.
  135. This time we'll hold on to the :class:`~celery.result.AsyncResult`::
  136. >>> result = add.delay(4, 4)
  137. Here's some examples of what you can do when you have results::
  138. >>> result.ready() # returns True if the task has finished processing.
  139. False
  140. >>> result.result # task is not ready, so no return value yet.
  141. None
  142. >>> result.get() # Waits until the task is done and returns the retval.
  143. 8
  144. >>> result.result # direct access to result, doesn't re-raise errors.
  145. 8
  146. >>> result.successful() # returns True if the task didn't end in failure.
  147. True
  148. If the task raises an exception, the return value of `result.successful()`
  149. will be :const:`False`, and `result.result` will contain the exception instance
  150. raised by the task.
  151. Where to go from here
  152. =====================
  153. After this you should read the :ref:`guide`. Specifically
  154. :ref:`guide-tasks` and :ref:`guide-executing`.