Celery + Django tutorial (Part 3).🥄Testing the taste

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

  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

🥄 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

2 thoughts on “Celery + Django tutorial (Part 3).🥄Testing the taste

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.