first-steps-with-django.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. =========================
  2. First steps with Django
  3. =========================
  4. Configuring your Django project to use Celery
  5. =============================================
  6. You only need three simple steps to use celery with your Django project.
  7. 1. Add ``celery`` to ``INSTALLED_APPS``.
  8. 2. Create the celery database tables::
  9. $ python manage.py syncdb
  10. 3. Configure celery to use the AMQP user and virtual host we created
  11. before, by adding the following to your ``settings.py``::
  12. BROKER_HOST = "localhost"
  13. BROKER_PORT = 5672
  14. BROKER_USER = "myuser"
  15. BROKER_PASSWORD = "mypassword"
  16. BROKER_VHOST = "myvhost"
  17. That's it.
  18. There are more options available, like how many processes you want to
  19. work in parallel (the ``CELERY_CONCURRENCY`` setting). You can also
  20. configure the backend used for storing task statuses. For now though,
  21. this should do. For all of the options available, please see the
  22. :doc:`configuration directive
  23. reference<../configuration>`.
  24. **Note:** If you're using SQLite as the Django database back-end,
  25. ``celeryd`` will only be able to process one task at a time, this is
  26. because SQLite doesn't allow concurrent writes.
  27. Running the celery worker server
  28. ================================
  29. To test this we'll be running the worker server in the foreground, so we can
  30. see what's going on without consulting the logfile::
  31. $ python manage.py celeryd
  32. However, in production you probably want to run the worker in the
  33. background as a daemon. To do this you need to use to tools provided by your
  34. platform. See :doc:`daemon mode reference<../cookbook/daemonizing>`.
  35. For a complete listing of the command line options available, use the help command::
  36. $ python manage.py help celeryd
  37. Defining and executing tasks
  38. ============================
  39. **Please note:** All the tasks have to be stored in a real module, they can't
  40. be defined in the python shell or ipython/bpython. This is because the celery
  41. worker server needs access to the task function to be able to run it.
  42. Put them in the ``tasks`` module of your Django application. The
  43. worker server will automatically load any ``tasks.py`` file for all
  44. of the applications listed in ``settings.INSTALLED_APPS``.
  45. Executing tasks using ``delay`` and ``apply_async`` can be done from the
  46. python shell, but keep in mind that since arguments are pickled, you can't
  47. use custom classes defined in the shell session.
  48. This is a task that adds two numbers:
  49. .. code-block:: python
  50. from celery.decorators import task
  51. @task()
  52. def add(x, y):
  53. return x + y
  54. To execute this task, we can use the ``delay`` method of the task class.
  55. This is a handy shortcut to the ``apply_async`` method which gives
  56. greater control of the task execution.
  57. See :doc:`Executing Tasks<../userguide/executing>` for more information.
  58. >>> from myapp.tasks import MyTask
  59. >>> MyTask.delay(some_arg="foo")
  60. At this point, the task has been sent to the message broker. The message
  61. broker will hold on to the task until a celery worker server has successfully
  62. picked it up.
  63. *Note:* If everything is just hanging when you execute ``delay``, please check
  64. that RabbitMQ is running, and that the user/password has access to the virtual
  65. host you configured earlier.
  66. Right now we have to check the celery worker log files to know what happened
  67. with the task. This is because we didn't keep the ``AsyncResult`` object
  68. returned by ``delay``.
  69. The ``AsyncResult`` lets us find the state of the task, wait for the task to
  70. finish and get its return value (or exception if the task failed).
  71. So, let's execute the task again, but this time we'll keep track of the task:
  72. >>> result = add.delay(4, 4)
  73. >>> result.ready() # returns True if the task has finished processing.
  74. False
  75. >>> result.result # task is not ready, so no return value yet.
  76. None
  77. >>> result.get() # Waits until the task is done and returns the retval.
  78. 8
  79. >>> result.result # direct access to result, doesn't re-raise errors.
  80. 8
  81. >>> result.successful() # returns True if the task didn't end in failure.
  82. True
  83. If the task raises an exception, the return value of ``result.successful()``
  84. will be ``False``, and ``result.result`` will contain the exception instance
  85. raised by the task.