Categories
Bangla Python Screencast

পাইথনে প্যাকেজ ইন্সটলেশন — পিপ

Categories
Python

Django & Celery – Easy async task processing

So, while developing a web application, there comes a time when we need to process some of the tasks in the background, perhaps asynchronously. For example, your user would upload photos and the app would post them to multiple social networks. We would definitely want to offload the uploading task to some background workers.

Django and Celery makes background task processing a breeze. In this article, we shall see how we can setup Django and Celery to start processing our background tasks. We would use Redis to maintain our task queue.

How does it work?

  • We define some tasks in our application. These tasks are expected to run for a pretty long time.
  • We run the celery workers. Celery knows how to find and load these tasks. The workers keep waiting on us.
  • We add some jobs to the workers queue from our web app. The workers now have something to work on. So they start taking the jobs from the queue and start processing them.
  • We can query the status of the jobs from our web app to know whats happening.
  • The easy to use Python API makes it really simple to use. You don’t need any specialisation or anything in Redis.

Setting Up

Let’s first install the Redis server:

The version that comes from Ubuntu official repo is quite old. You can install the latest version from 3rd party PPAs.

Install Celery with Redis support:

And then install django-celery package:

Configuration

Add “djcelery” to your installed apps list:

Modify your main app’s settings.py file to add the celery specific settings:

Now, inside your main application directory (the directory in which settings.py is located), create a file named “celery.py” with these contents:

The above codes do a few things:

  • It creates our own Celery instance.
  • We ask the celery instance to load necessary configs from our project’s settings file.
  • We make the instance auto discover tasks from our INSTALLED_APPS.

Also let’s modify the “__init__.py” file in the same directory to make the celery app available more easily:

This would allow us to use the same app instance for shared tasks across reusable django apps.

Defining Tasks

Now let’s create a tasks.py file in one of our INSTALLED_APPS and add these contents:

Now we have defined our own celery app, we have our tasks. It’s now time to launch the workers and start adding tasks.

Processing Tasks

Before we can start processing tasks, we have to launch the celery daemon first. This is how we do it:

Here, we tell celery to use the celery instance we defined and configured earlier. Here “project” is the main app, the package that contains our settings.py along with celery.py. The “app” the variable name which holds the celery instance.

Now let’s use the Django shell to add and query jobs:

So as we can see, out task was processed by celery. And we could easily query the status. We would generally use the meta data to store any task related information.

Categories
Bangla Python Screencast

Object Oriented Programming in Python: Bangla Screencast

Video is available in HD

সংযুক্তি – ১

স্ক্রীনকাস্টে প্রাইভেট ভ্যারিয়েবল বা ডাটা হাইডিং এর উদাহরন দেখানোর সময় ক্লাস লেভেল ভ্যারিয়েবল ব্যবহার করা হয়েছে । ইনস্ট্যান্স ভ্যারিয়েবল এর ক্ষেত্রেও একই নিয়ম প্রযোজ্য । এখানে কোড স্যাম্পল দেওয়া হলো:

সংযুক্তি – ২

ক্লাস নির্ভর মেটাক্লাস ইম্প্লিমেন্টেশন এবং একটি কাল্পনিক প্রয়োগ দেখার জন্য এই ভিডিও টি দেখতে পারেন – Python – Metaprogramming