first-steps-with-django.rst 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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('django.conf: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. You can pass the object directly here, but using a string is better since
  44. then the worker doesn't have to serialize the object when using Windows
  45. or execv:
  46. .. code-block:: python
  47. app.config_from_object('django.conf:settings')
  48. Next, a common practice for reusable apps is to define all tasks
  49. in a separate ``tasks.py`` module, and Celery does have a way to
  50. autodiscover these modules:
  51. .. code-block:: python
  52. app.autodiscover_tasks(settings.INSTALLED_APPS, related_name='tasks')
  53. With the line above Celery will automatically discover tasks in reusable
  54. apps if you follow the ``tasks.py`` convention::
  55. - app1/
  56. - app1/tasks.py
  57. - app2/models.py
  58. - app2/
  59. - app2/tasks.py
  60. - app2/models.py
  61. This way you do not have to manually add the individual modules
  62. to the :setting:`CELERY_IMPORTS` setting.
  63. Finally, the ``debug_task`` example is a task that dumps
  64. its own request information. This is using the new ``bind=True`` task option
  65. introduced in Celery 3.1 to easily refer to the current task instance.
  66. The `celery` command
  67. --------------------
  68. To use the :program:`celery` command with Django you need to
  69. set up the :envvar:`DJANGO_SETTINGS_MODULE` environment variable:
  70. .. code-block:: bash
  71. $ DJANGO_SETTINGS_MODULE='proj.settings' celery -A proj worker -l info
  72. $ DJANGO_SETTINGS_MODULE='proj.settings' celery -A proj status
  73. If you find this inconvienient you can create a small wrapper script
  74. alongside ``manage.py`` that automatically binds to your app, e.g. ``proj/celery.py``
  75. :file:`proj/celery.py`
  76. .. code-block:: python
  77. #!/usr/bin/env python
  78. import os
  79. from proj.celery import celery
  80. if __name__ == '__main__':
  81. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.celery')
  82. celery.start()
  83. Then you can use this command directly:
  84. .. code-block:: bash
  85. $ ./celery.py status
  86. Using the Django ORM/Cache as a result backend.
  87. -----------------------------------------------
  88. The ``django-celery`` library defines result backends that
  89. uses the Django ORM and Django Cache frameworks.
  90. To use this with your project you need to follow these three steps:
  91. 1. Install the ``django-celery`` library:
  92. .. code-block:: bash
  93. $ pip install django-celery
  94. 2. Add ``djcelery`` to ``INSTALLED_APPS``.
  95. 3. Create the celery database tables.
  96. This step will create the tables used to store results
  97. when using the database result backend and the tables used
  98. by the database periodic task scheduler. You can skip
  99. this step if you don't use these.
  100. If you are using south_ for schema migrations, you'll want to:
  101. .. code-block:: bash
  102. $ python manage.py migrate djcelery
  103. For those who are not using south, a normal ``syncdb`` will work:
  104. .. code-block:: bash
  105. $ python manage.py syncdb
  106. .. _south: http://pypi.python.org/pypi/South/
  107. .. admonition:: Relative Imports
  108. You have to be consistent in how you import the task module, e.g. if
  109. you have ``project.app`` in ``INSTALLED_APPS`` then you also
  110. need to import the tasks ``from project.app`` or else the names
  111. of the tasks will be different.
  112. See :ref:`task-naming-relative-imports`
  113. Starting the worker process
  114. ===========================
  115. In a production environment you will want to run the worker in the background
  116. as a daemon - see :ref:`daemonizing` - but for testing and
  117. development it is useful to be able to start a worker instance by using the
  118. ``celery worker`` manage command, much as you would use Django's runserver:
  119. .. code-block:: bash
  120. $ DJANGO_SETTINGS_MODULE='proj.settings' celery -A proj worker -l info
  121. For a complete listing of the command-line options available,
  122. use the help command:
  123. .. code-block:: bash
  124. $ celery help
  125. Where to go from here
  126. =====================
  127. If you want to learn more you should continue to the
  128. :ref:`Next Steps <next-steps>` tutorial, and after that you
  129. can study the :ref:`User Guide <guide>`.