first-steps-with-django.rst 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. =========================
  2. First steps with Django
  3. =========================
  4. Using Celery with Django
  5. ========================
  6. To use Celery with your Django project you must first define
  7. an instance of the Celery library.
  8. If you have a modern Django project layout like
  9. - proj/
  10. - proj/__init__.py
  11. - proj/settings.py
  12. - proj/urls.py
  13. - manage.py
  14. then the recommended way is to create a new `proj/proj/celery.py` module
  15. that defines the Celery instance:
  16. :file: `proj/proj/celery.py`
  17. .. code-block:: python
  18. from celery import Celery
  19. from django.conf import settings
  20. celery = Celery('proj.celery')
  21. celery.config_from_object(settings)
  22. celery.autodiscover_tasks(settings.INSTALLED_APPS, related_name='tasks')
  23. @celery.task(bind=True)
  24. def debug_task(self):
  25. print('Request: {0!r}'.format(self.request))
  26. Let's explain what happens here.
  27. First we create the Celery app instance:
  28. .. code-block:: python
  29. celery = Celery('proj')
  30. Then we add the Django settings module as a configuration source
  31. for Celery. This means that you don't have to use multiple
  32. configuration files, and instead configure Celery directly
  33. from the Django settings.
  34. .. code-block:: python
  35. celery.config_from_object(settings)
  36. Next, a common practice for reusable apps is to define all tasks
  37. in a separate ``tasks.py`` module, and Celery does have a way to
  38. autodiscover these modules:
  39. .. code-block:: python
  40. celery.autodiscover_tasks(settings.INSTALLED_APPS, related_name='tasks')
  41. With the line above Celery will automatically discover tasks in reusable
  42. apps if you follow the ``tasks.py`` convention::
  43. - app1/
  44. - app1/tasks.py
  45. - app2/models.py
  46. - app2/
  47. - app2/tasks.py
  48. - app2/models.py
  49. This way you do not have to manually add the individual modules
  50. to the :setting:`CELERY_IMPORTS` setting.
  51. Finally, the ``debug_task`` example is a task that dumps
  52. its own request information. This is using the new ``bind=True`` task option
  53. introduced in Celery 3.1 to easily refer to the current task instance.
  54. The `celery` command
  55. --------------------
  56. To use the :program:`celery` command with Django you need to
  57. set up the :envvar:`DJANGO_SETTINGS_MODULE` environment variable:
  58. .. code-block:: bash
  59. $ DJANGO_SETTINGS_MODULE='proj.settings' celery -A proj worker -l info
  60. $ DJANGO_SETTINGS_MODULE='proj.settings' celery -A proj status
  61. If you find this inconvienient you can create a small wrapper script
  62. alongside ``manage.py`` that automatically binds to your app, e.g. ``proj/celery.py`
  63. :file:`proj/celery.py`
  64. .. code-block:: python
  65. #!/usr/bin/env python
  66. import os
  67. from proj.celery import celery
  68. if __name__ == '__main__':
  69. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.celery')
  70. celery.start()
  71. Then you can use this command directly:
  72. .. code-block:: bash
  73. $ ./celery.py status
  74. Using the Django ORM/Cache as a result backend.
  75. -----------------------------------------------
  76. The ``django-celery`` library defines result backends that
  77. uses the Django ORM and Django Cache frameworks.
  78. To use this with your project you need to follow these three steps:
  79. 1. Install the ``django-celery`` library:
  80. .. code-block:: bash
  81. $ pip install django-celery
  82. 2. Add ``djcelery`` to ``INSTALLED_APPS``.
  83. 3. Create the celery database tables.
  84. If you are using south_ for schema migrations, you'll want to:
  85. .. code-block:: bash
  86. $ python manage.py migrate djcelery
  87. For those who are not using south, a normal ``syncdb`` will work:
  88. .. code-block:: bash
  89. $ python manage.py syncdb
  90. .. _south: http://pypi.python.org/pypi/South/
  91. .. admonition:: Relative Imports
  92. You have to be consistent in how you import the task module, e.g. if
  93. you have ``project.app`` in ``INSTALLED_APPS`` then you also
  94. need to import the tasks ``from project.app`` or else the names
  95. of the tasks will be different.
  96. See :ref:`task-naming-relative-imports`
  97. Starting the worker process
  98. ===========================
  99. In a production environment you will want to run the worker in the background
  100. as a daemon - see :ref:`daemonizing` - but for testing and
  101. development it is useful to be able to start a worker instance by using the
  102. ``celery worker`` manage command, much as you would use Django's runserver:
  103. .. code-block:: bash
  104. $ DJANGO_SETTINGS_MODULE='proj.settings' celery -A proj worker -l info
  105. For a complete listing of the command-line options available,
  106. use the help command:
  107. .. code-block:: bash
  108. $ celery help
  109. Where to go from here
  110. =====================
  111. If you want to learn more you should continue to the
  112. :ref:`Next Steps <next-steps>` tutorial, and after that you
  113. can study the :ref:`User Guide <guide>`.