first-steps-with-django.rst 5.5 KB

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