Forgot password?
Don't already have an account? Create an account.
By creating an Ordinary Coders account, you agree to Ordinary Coders' Terms and Conditions.
If you already have an account, login instead.
Introduction to Asynchronous tasks in Django
If you are reading this, chances are you're familiar with the Django framework. If not feel free to check out some of our Beginner's Guide to Django Web Apps. In this post, we discuss how to set up asynchronous background tasks using Celery, Redis, and a monitoring tool called Flower using the Django framework. Asynchronous tasks signify that multiple tasks are processed in parallel whereas synchronous tasks are only executed one by one. Celery is a task queue written in Python that allows work to be distributed amongst workers, thus enabling tasks to be executed asynchronously. However, Celery requires a message broker that acts as an intermediary between the Django application and the Celery task queue. For this tutorial, we will use Redis as our message broker. Lastly, to view our workers and see the tasks we complete, we will use Flower. Below we list a summary of the various features and official websites for each piece of software.
Celery Features
Redis Features
Flower Features
Flower Preview:
Django Celery Redis Tutorial:
For this tutorial, we will simply be creating a background task that takes in an argument and prints a string containing the argument when the task is executed. Of course, background tasks have many other use cases, such as sending emails, converting images to smaller thumbnails, and scheduling periodic tasks.
C:\Users\Owner\Desktop\code>py -m venv celery-demo C:\Users\Owner\Desktop\code>cd celery-demo C:\Users\Owner\Desktop\code\celery-demo>ls Include Lib Scripts pyvenv.cfg C:\Users\Owner\Desktop\code\celery-demo>Scripts\activate (celery-demo) C:\Users\Owner\Desktop\code\celery-demo>pip install django (celery-demo) C:\Users\Owner\Desktop\code\celery-demo>django-admin startproject mysite
(celery-demo) C:\Users\Owner\Desktop\code\celery-demo\mysite>pip install "celery[redis]"
import os from celery import Celery # set the default Django settings module for the 'celery' program. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') app = Celery('mysite', backend='redis', broker='redis://localhost:6379') # Using a string here means the worker doesn't have to serialize # the configuration object to child processes. # - namespace='CELERY' means all celery-related configuration keys # should have a `CELERY_` prefix. app.config_from_object('django.conf:settings', namespace='CELERY') # Load task modules from all registered Django app configs. app.autodiscover_tasks() @app.task(bind=True) def debug_task(self): print('Request: {0!r}'.format(self.request))
from __future__ import absolute_import, unicode_literals # This will make sure the app is always imported when # Django starts so that shared_task will use this app. from .celery import app as celery_app __all__ = ('celery_app',)
BROKER_URL = 'redis://localhost:6379' CELERY_RESULT_BACKEND = 'redis://localhost:6379' CELERY_ACCEPT_CONTENT = ['application/json'] CELERY_TASK_SERIALIZER = 'json' CELERY_RESULT_SERIALIZER = 'json' INSTALLED_APPS = [ # others app 'celery', ]
app.autodiscover_tasks()
(celery-demo) C:\Users\Owner\Desktop\code\celery-demo\mysite>py manage.py startapp main
@shared_task
#mysite/main/tasks.py from __future__ import absolute_import from celery import shared_task @shared_task def test(param): return 'The tasks executed with the following parameter: "%s" '
#mysite/main/urls.py from django.contrib import admin from django.urls import path, include from . import views urlpatterns = [ path('', views.home ), ]
delay
#mysite/main/views.py from django.shortcuts import render from django.http import HttpResponse from .tasks import test # Create your views here. def home(request): test.delay(15) return HttpResponse("Hey there!")
redis-server.exe
redis-client.exe
brew install redis
redis-client
py/python3 manage.py runserver
celery -A mysite worker -l info --pool=solo
flower -A mysite
(celery-demo) C:\Users\Owner\Desktop\code\celery-demo\mysite>flower -A mysite [I 200914 08:03:54 command:140] Visit me at http://localhost:5555 [I 200914 08:03:54 command:145] Broker: redis://localhost:6379//
[2020-09-21 12:12:23,328: INFO/MainProcess] Connected to redis://localhost:6379// [2020-09-21 12:12:23,338: INFO/MainProcess] mingle: searching for neighbors [2020-09-21 12:12:24,367: INFO/MainProcess] mingle: all alone [2020-09-21 12:12:24,376: WARNING/MainProcess] c:\users\owner\desktop\code\celery-demo\lib\site-packages\celery\fixups\django.py:206: UserWarning: Using settings.DEBUG leads to a memory leak, never use this setting in production environments! leak, never use this setting in production environments!''') [2020-09-21 12:12:24,377: INFO/MainProcess] celery@DESKTOP-9ERS2HA ready. [2020-09-21 12:12:37,121: INFO/MainProcess] Events of group {task} enabled by remote. [2020-09-21 12:12:40,825: INFO/MainProcess] Received task: main.tasks.test[4b88a8e4-9efa-4d7c-82ff-bfffa5969c4c] [2020-09-21 12:12:40,829: INFO/MainProcess] Task main.tasks.test[4b88a8e4-9efa-4d7c-82ff-bfffa5969c4c] succeeded in 0.0s: 'The tasks executed with the following parameter:15'
Conclusion: Benefits of Asynchronous Tasks
While this was a rather simple tutorial on scheduling background/asynchronous tasks, there are plenty of opportunities moving forwards. Think about Twitter or Instagram and how users rarely need to reload a given page. Instead, these apps "feel alive" as likes, retweets, and shares update nearly in real-time. Asynchronous tasks allow applications to provide smoother user experiences since users can constantly engage with updated content.
Django Powered Blog for Affiliate Marketing
A Django powered blog and product showcase for affiliate marketing from Building a Django Web App course. Pre-built Django …
Follow us @ordinarycoders
Post a Comment Join the community
@ordinarycoders
April 25, 2020, 2:43 p.m.
April 14, 2021, 10:03 a.m.
ordinarycoders_bot
March 29, 2021, 7:30 p.m.
Jaysha
March 6, 2021, 2:47 p.m.
James
March 3, 2021, 6:04 p.m.
Jan. 5, 2021, 4:39 p.m.