Here is a short part three about testing your Celery tasks. Do they even work??
Previous part: Part 2 🍮 Results pudding
Next part: Part 4 🍻 Serving Celery workers
Following stack: Django + Celery + Nginx
Project structure
proj
proj
__init__.py
settings.py
urls.py
...
celery.py
firstapp
tasks.py
Content table
🍜 Cooking project dinner
- 🥩 Django steak medium rare
- 🥗 Broker salad
- 🥬 Chopped Redis
- 🥕 Carrot for Rabbitmq
- 🥒 Pickled tasks
- 🥗 Broker salad
- 🍮 Results pudding
- 🍪 HTML
- 🍯 View
- 🍫 Url
- 🥄 Testing the taste
- 🍻 Serving Celery workers
- 🍱 Pack Linux with Celery Daemon
🍜 Cooking project dinner
🥄 3. Testing the taste
from .tasks import *
To make Celery do the task, run the method, but don’t forget to add .delay(*args)
. Like this:
add.delay(1111, 9999)
So at some point, a Celery worker will print:
Result 1111 + 9999 = 11110
So in the shell I delayed several add() and here how it is in the Django shell:
>>> add.delay(1111, 9999)
<AsyncResult: a0fb8760-e495-46d8-8987-9163c0cca3c4>
>>> add.delay(2222, 8888)
<AsyncResult: 9cf2853f-5125-4082-81fa-1884f3509e84>
>>> add.delay(3333, 7777)
<AsyncResult: b387181d-3e58-4363-8c2a-ece243c291b2>
>>> add.delay(4444, 6666)
<AsyncResult: a2857372-3273-4b21-8d15-f4d66b4f801c>
>>> add.delay(5555, 5555)
<AsyncResult: 0be2a61e-c3df-4586-9442-1facafeda4a8>
In the post about collecting static, I introduced a small bash script that you can use this celery command in. Insert the command anywhere before deactivate
:
#! /bin/bash
source $1
python relative_path/to/collect_static.py
celery -A proj worker -l info
deactivate
And activate it:
bash ./path/to/smart_collect /path/to/myvirtualenv/bin/activate
Next part: 🍻 Serving Celery workers
[…] Next part: Part 3 🥄 Testing the taste […]
LikeLike
[…] Previous part: Part 3 🥄 Testing the taste […]
LikeLike