|
| 1 | +============================================================== |
| 2 | + Example Django project using Celery |
| 3 | +============================================================== |
| 4 | + |
| 5 | +* For sqlite testing, we can implement migration through the docker file via adding another command. `python manage.py makemigrations` and `python manage.py migrate` |
| 6 | + |
| 7 | +Credits: Originally from https://github.com/celery/celery/tree/master/examples/django |
| 8 | + |
| 9 | +Modified the app file just to add a hello-world end point (/api-check/) for validation. |
| 10 | + |
| 11 | +Contents |
| 12 | +======== |
| 13 | + |
| 14 | +``proj/`` |
| 15 | +--------- |
| 16 | + |
| 17 | +This is a project in itself, created using |
| 18 | +``django-admin.py startproject proj``, and then the settings module |
| 19 | +(``proj/settings.py``) was modified to add ``demoapp`` to |
| 20 | +``INSTALLED_APPS`` |
| 21 | + |
| 22 | +``proj/celery.py`` |
| 23 | +---------- |
| 24 | + |
| 25 | +This module contains the Celery application instance for this project, |
| 26 | +we take configuration from Django settings and use ``autodiscover_tasks`` to |
| 27 | +find task modules inside all packages listed in ``INSTALLED_APPS``. |
| 28 | + |
| 29 | +``demoapp/`` |
| 30 | +------------ |
| 31 | + |
| 32 | +Example generic app. This is decoupled from the rest of the project by using |
| 33 | +the ``@shared_task`` decorator. This decorator returns a proxy that always |
| 34 | +points to the currently active Celery instance. |
| 35 | + |
| 36 | +Installing requirements |
| 37 | +======================= |
| 38 | + |
| 39 | +The settings file assumes that ``rabbitmq-server`` is running on ``localhost`` |
| 40 | +using the default ports. More information here: |
| 41 | + |
| 42 | +http://docs.celeryproject.org/en/latest/getting-started/brokers/rabbitmq.html |
| 43 | + |
| 44 | +In addition, some Python requirements must also be satisfied: |
| 45 | + |
| 46 | +.. code-block:: console |
| 47 | +
|
| 48 | + $ pip install -r requirements.txt |
| 49 | +
|
| 50 | +Starting the worker |
| 51 | +=================== |
| 52 | + |
| 53 | +.. code-block:: console |
| 54 | +
|
| 55 | + $ celery -A proj worker -l INFO |
| 56 | +
|
| 57 | +Running a task |
| 58 | +=================== |
| 59 | + |
| 60 | +.. code-block:: console |
| 61 | +
|
| 62 | + $ python ./manage.py shell |
| 63 | + >>> from demoapp.tasks import add, mul, xsum |
| 64 | + >>> res = add.delay(2,3) |
| 65 | + >>> res.get() |
| 66 | + 5 |
0 commit comments