| 1 |
============================================================== |
| 2 |
Example Django project using Celery |
| 3 |
============================================================== |
| 4 |
|
| 5 |
Contents |
| 6 |
======== |
| 7 |
|
| 8 |
``proj/`` |
| 9 |
--------- |
| 10 |
|
| 11 |
This is a project in itself, created using |
| 12 |
``django-admin.py startproject proj``, and then the settings module |
| 13 |
(``proj/settings.py``) was modified to add ``demoapp`` to |
| 14 |
``INSTALLED_APPS`` |
| 15 |
|
| 16 |
``proj/celery.py`` |
| 17 |
---------- |
| 18 |
|
| 19 |
This module contains the Celery application instance for this project, |
| 20 |
we take configuration from Django settings and use ``autodiscover_tasks`` to |
| 21 |
find task modules inside all packages listed in ``INSTALLED_APPS``. |
| 22 |
|
| 23 |
``demoapp/`` |
| 24 |
------------ |
| 25 |
|
| 26 |
Example generic app. This is decoupled from the rest of the project by using |
| 27 |
the ``@shared_task`` decorator. This decorator returns a proxy that always |
| 28 |
points to the currently active Celery instance. |
| 29 |
|
| 30 |
Installing requirements |
| 31 |
======================= |
| 32 |
|
| 33 |
The settings file assumes that ``rabbitmq-server`` is running on ``localhost`` |
| 34 |
using the default ports. More information here: |
| 35 |
|
| 36 |
https://docs.celeryq.dev/en/latest/getting-started/brokers/rabbitmq.html |
| 37 |
|
| 38 |
In addition, some Python requirements must also be satisfied: |
| 39 |
|
| 40 |
.. code-block:: console |
| 41 |
|
| 42 |
$ pip install -r requirements.txt |
| 43 |
|
| 44 |
Starting the worker |
| 45 |
=================== |
| 46 |
|
| 47 |
.. code-block:: console |
| 48 |
|
| 49 |
$ celery -A proj worker -l INFO |
| 50 |
|
| 51 |
Running a task |
| 52 |
=================== |
| 53 |
|
| 54 |
.. code-block:: console |
| 55 |
|
| 56 |
$ python ./manage.py shell |
| 57 |
>>> from demoapp.tasks import add, mul, xsum |
| 58 |
>>> res = add.delay_on_commit(2, 3) |
| 59 |
>>> res.get() |
| 60 |
5 |
| 61 |
|
| 62 |
.. note:: |
| 63 |
|
| 64 |
The ``delay_on_commit`` method is only available when using Django, |
| 65 |
and was added in Celery 5.4. If you are using an older version of Celery, |
| 66 |
you can use ``delay`` instead. |