first-steps-with-django.rst 4.2 KB

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