Here is part four about testing your Celery tasks.
Previous part: Part 3 🥄 Testing the taste
Next part: Part 5 🍱 Pack Linux with Celery Daemon
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
🍻 4. Serving Celery workers
Celery workers run using this command in your environment:
celery -A proj worker -l info
Here proj
stands for your Django project name. For more information about this command, you can use celery help
.
To run tests, make a Python script and run it through shell. Like this:
# relativepath/to/mytextfile.py
from firstapp.tasks import add
add.delay()
So then you pass the script to shell:
shell --settings=mytestsettings < relativepath/to/mytextfile.py
For example, running the add()
tasks you may have something like this:
[2019-09-29 16:33:47,723: INFO/MainProcess] Received task: firstapp.tasks.add[8ff0726b-e550-4a22-9fea-3631dd78453f]
[2019-09-29 16:33:47,727: INFO/ForkPoolWorker-4] Task firstapp.tasks.add[8ff0726b-e550-4a22-9fea-3631dd78453f] succeeded in 0.0027555239939829335s: 'Result 1111+9999=11110'
Possible errors
1. Testing in shell
.
If you have different settings file for development and live server, and your line server file is your default one, which is read by Django without --settings
, celery will eat even if you runshell
with --settings=mytestsettings
. That means that the way you start the celery does not takes this into account. Quick solution is to copy the live settings.py and make your mytestsettings.py the default settings.py.
2. AttributeError: ‘function’ object has no attribute ‘delay’
It must be conflicting names. If you use Pycharm, click on the function name with pressed Ctrl
and you will see what other functions there are with the same name.
Next part 🍱 Pack Linux with Celery Daemon
[…] Next part: Part 4 🍻 Serving Celery workers […]
LikeLike