first-steps-with-celery.rst 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. .. _tut-celery:
  2. ========================
  3. First steps with Celery
  4. ========================
  5. .. contents::
  6. :local:
  7. .. _celerytut-simple-tasks:
  8. Creating a simple task
  9. ======================
  10. In this tutorial we are creating a simple task that adds two
  11. numbers. Tasks are defined in normal Python modules.
  12. By convention we will call our module :file:`tasks.py`, and it looks
  13. like this:
  14. :file: `tasks.py`
  15. .. code-block:: python
  16. from celery.decorators import task
  17. @task
  18. def add(x, y):
  19. return x + y
  20. All Celery tasks are classes that inherits from the
  21. :class:`~celery.task.base.Task` class. In this example we're using a
  22. decorator that wraps the add function in an appropriate class for us
  23. automatically.
  24. .. seealso::
  25. The full documentation on how to create tasks and task classes is in the
  26. :doc:`../userguide/tasks` part of the user guide.
  27. .. _celerytut-conf:
  28. Configuration
  29. =============
  30. Celery is configured by using a configuration module. By default
  31. this module is called :file:`celeryconfig.py`.
  32. The configuration module must either be in the current directory
  33. or on the Python path, so that it can be imported.
  34. You can also set a custom name for the configuration module by using
  35. the :envvar:`CELERY_CONFIG_MODULE` environment variable.
  36. Let's create our :file:`celeryconfig.py`.
  37. 1. Configure how we communicate with the broker (RabbitMQ in this example)::
  38. BROKER_HOST = "localhost"
  39. BROKER_PORT = 5672
  40. BROKER_USER = "myuser"
  41. BROKER_PASSWORD = "mypassword"
  42. BROKER_VHOST = "myvhost"
  43. 2. Define the backend used to store task metadata and return values::
  44. CELERY_RESULT_BACKEND = "amqp"
  45. The AMQP backend is non-persistent by default, and you can only
  46. fetch the result of a task once (as it's sent as a message).
  47. For list of backends available and related options see
  48. :ref:`conf-result-backend`.
  49. 3. Finally we list the modules the worker should import. This includes
  50. the modules containing your tasks.
  51. We only have a single task module, :file:`tasks.py`, which we added earlier::
  52. CELERY_IMPORTS = ("tasks", )
  53. That's it.
  54. There are more options available, like how many processes you want to
  55. use to process work in parallel (the :setting:`CELERY_CONCURRENCY` setting),
  56. and we could use a persistent result store backend, but for now, this should
  57. do. For all of the options available, see :ref:`configuration`.
  58. .. note::
  59. You can also specify modules to import using the :option:`-I` option to
  60. :mod:`~celery.bin.celeryd`::
  61. $ celeryd -l info -I tasks,handlers
  62. This can be a single, or a comma separated list of task modules to import
  63. when :program:`celeryd` starts.
  64. .. _celerytut-running-celeryd:
  65. Running the celery worker server
  66. ================================
  67. To test we will run the worker server in the foreground, so we can
  68. see what's going on in the terminal::
  69. $ celeryd --loglevel=INFO
  70. In production you will probably want to run the worker in the
  71. background as a daemon. To do this you need to use the tools provided
  72. by your platform, or something like `supervisord`_ (see :ref:`daemonization`
  73. for more information).
  74. For a complete listing of the command line options available, do::
  75. $ celeryd --help
  76. .. _`supervisord`: http://supervisord.org
  77. .. _celerytut-executing-task:
  78. Executing the task
  79. ==================
  80. Whenever we want to execute our task, we use the
  81. :meth:`~celery.task.base.Task.delay` method of the task class.
  82. This is a handy shortcut to the :meth:`~celery.task.base.Task.apply_async`
  83. method which gives greater control of the task execution (see
  84. :ref:`guide-executing`).
  85. >>> from tasks import add
  86. >>> add.delay(4, 4)
  87. <AsyncResult: 889143a6-39a2-4e52-837b-d80d33efb22d>
  88. At this point, the task has been sent to the message broker. The message
  89. broker will hold on to the task until a worker server has consumed and
  90. executed it.
  91. Right now we have to check the worker log files to know what happened
  92. with the task. This is because we didn't keep the
  93. :class:`~celery.result.AsyncResult` object returned.
  94. The :class:`~celery.result.AsyncResult` lets us check the state of the task,
  95. wait for the task to finish, get its return value or exception/traceback
  96. if the task failed, and more.
  97. Let's execute the task again -- but this time we'll keep track of the task
  98. by holding on to the :class:`~celery.result.AsyncResult`::
  99. >>> result = add.delay(4, 4)
  100. >>> result.ready() # returns True if the task has finished processing.
  101. False
  102. >>> result.result # task is not ready, so no return value yet.
  103. None
  104. >>> result.get() # Waits until the task is done and returns the retval.
  105. 8
  106. >>> result.result # direct access to result, doesn't re-raise errors.
  107. 8
  108. >>> result.successful() # returns True if the task didn't end in failure.
  109. True
  110. If the task raises an exception, the return value of `result.successful()`
  111. will be :const:`False`, and `result.result` will contain the exception instance
  112. raised by the task.
  113. Where to go from here
  114. =====================
  115. After this you should read the :ref:`guide`. Specifically
  116. :ref:`guide-tasks` and :ref:`guide-executing`.