Celery + Django tutorial (Part 4). 🍻Serving Celery workers

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

  1. 🥩 Django steak medium rare
    1. 🥗 Broker salad
      1. 🥬 Chopped Redis
      2. 🥕 Carrot for Rabbitmq
    2. 🥒 Pickled tasks
  2. 🍮 Results pudding
    1. 🍪 HTML
    2. 🍯 View
    3. 🍫 Url
  3. 🥄 Testing the taste
  4. 🍻 Serving Celery workers
  5. 🍱 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

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

One thought on “Celery + Django tutorial (Part 4). 🍻Serving Celery workers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.