first-steps-with-django.rst 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. If you are using south_ for schema migrations, you'll want to:
  86. .. code-block:: bash
  87. $ python manage.py migrate djcelery
  88. For those who are not using south, a normal ``syncdb`` will work:
  89. .. code-block:: bash
  90. $ python manage.py syncdb
  91. .. _south: http://pypi.python.org/pypi/South/
  92. .. admonition:: Relative Imports
  93. You have to be consistent in how you import the task module, e.g. if
  94. you have ``project.app`` in ``INSTALLED_APPS`` then you also
  95. need to import the tasks ``from project.app`` or else the names
  96. of the tasks will be different.
  97. See :ref:`task-naming-relative-imports`
  98. Starting the worker process
  99. ===========================
  100. In a production environment you will want to run the worker in the background
  101. as a daemon - see :ref:`daemonizing` - but for testing and
  102. development it is useful to be able to start a worker instance by using the
  103. ``celery worker`` manage command, much as you would use Django's runserver:
  104. .. code-block:: bash
  105. $ DJANGO_SETTINGS_MODULE='proj.settings' celery -A proj worker -l info
  106. For a complete listing of the command-line options available,
  107. use the help command:
  108. .. code-block:: bash
  109. $ celery help
  110. Where to go from here
  111. =====================
  112. If you want to learn more you should continue to the
  113. :ref:`Next Steps <next-steps>` tutorial, and after that you
  114. can study the :ref:`User Guide <guide>`.