Working with Celery

Celery is a feature-rich asynchronous task queue manager. Here, a task refers to a callable that, when executed, will perform the activity asynchronously. Celery is used in production by several well-known organizations including Instagram and Mozilla, for handling millions of tasks a day.

While installing Celery, you will need to pick and choose various components such as a broker and result store. If you are confused, I would recommend installing Redis and skipping a result store for starters. As Redis works in-memory, if your messages are larger and need persistence, you should use RabbitMQ instead. You can follow the First Steps with Celery and Using Celery with Django topics in the Celery User Guide to get started.

In Django, Celery jobs are usually mentioned in a separate file named tasks.py within the respective app directory.

Here's what a typical Celery task looks like:

# tasks.py 
@shared_task 
def fetch_feed(feed_id): 
    feed_obj = models.Feed.objects.get(id=feed_id) 
    feed_obj.page = retrieve_page(feed_obj.feed_url) 
    feed_obj.retrieved = timezone.now() 
    feed_obj.save() 

This task retrieves the content of an RSS feed and saves it to the database.

It looks like a normal Python function (even though it will be internally wrapped by a class), except for the @shared_task decorator. This defines a Celery task. A shared task can be used by other apps within the same project. It makes the task reusable by creating independent instances of the task in each registered app.

To invoke this task, you can use the delay() method, as follows:

>>> from tasks import fetch_feed 
>>> fetch_feed.delay(feed_id=some_feed.id)  

Unlike a normal function call, the execution does not jump to fetch_feed or block until the function returns. Instead, it returns immediately with an AsyncResult instance. This can be used to check the status and return value of the task.

To find out how and when it is invoked, let's look at how Celery works.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.186.201