first-steps-with-django.rst 5.0 KB

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