Why is Celery useful?
Task queues and the Celery implementation in particular are one of the trickier parts of a Python web application stack to understand.
If you are a junior developer it can be unclear why moving work outside the HTTP request-response cycle is important. In short, you want your WSGI server to respond to incoming requests as quickly as possible because each request ties up a worker process until the response is finished. Moving work off those workers by spinning up asynchronous jobs as tasks in a queue is a straightforward way to improve WSGI server response times.
What’s the difference between Celeryd and Celerybeat?
Celery can be used to run batch jobs in the background on a regular schedule. A key concept in Celery is the difference between the Celery daemon (celeryd), which executes tasks, Celerybeat, which is a scheduler. Think of Celeryd as a tunnel-vision set of one or more workers that handle whatever tasks you put in front of them. Each worker will perform a task and when the task is completed will pick up the next one. The cycle will repeat continously, only waiting idly when there are no more tasks to put in front of them.
Celerybeat on the other hand is like a boss who keeps track of when tasks should be executed. Your application can tell Celerybeat to execute a task at time intervals, such as every 5 seconds or once a week. Celerybeat can also be instructed to run tasks on a specific date or time, such as 5:03pm every Sunday. When the interval or specific time is hit, Celerybeat will hand the job over to Celeryd to execute on the next available worker.
Celery is a powerful tool that can be difficult to wrap your mind around at first. Be sure to read up on task queue concepts then dive into these specific Celery tutorials.
- Getting Started Scheduling Tasks with Celery is a detailed walkthrough for setting up Celery with Django (although Celery can also be used without a problem with other frameworks).
- Introducing Celery for Python+Django provides an introduction to the Celery task queue with Django as the intended framework for building a web application.
- How to use Celery with RabbitMQ is a detailed walkthrough for using these tools on an Ubuntu VPS.
- Celery – Best Practices explains things you should not do with Celery and shows some underused features for making task queues easier to work with.
- Celery Best Practices is a different author’s follow up to the above best practices post that builds upon some of his own learnings from 3+ years using Celery.
- Asynchronous Processing in Web Applications Part One and Part Two are great reads for understanding the difference between a task queue and why you shouldn’t use your database as one.
- A 4 Minute Intro to Celery is a short introductory task queue screencast.
- Heroku wrote about how to secure Celery when tasks are otherwise sent over unencrypted networks.
- Miguel Grinberg wrote a nice post on using the task queue Celery with Flask. He gives an overview of Celery followed by specific code to set up the task queue and integrate it with Flask.
- 3 Gotchas for Working with Celery are things to keep in mind when you’re new to the Celery task queue implementation.
- Deferred Tasks and Scheduled Jobs with Celery 3.1, Django 1.7 and Redis is a video along with code that shows how to set up Celery with Redis as the broker in a Django application.
- Setting up an asynchronous task queue for Django using Celery and Redis is a straightforward tutorial for setting up the Celery task queue for Django web applications using the Redis broker on the back end.
- Asynchronous Tasks With Django and Celery shows how to integrate Celery with Django and create Periodic Tasks.
- Dask and Celery compares Dask.distributed with Celery for Python projects. The post gives code examples to show how to execute tasks with either task queue.
Celery deployment resources
- The “Django in Production” series by Rob Golding contains a post specifically on Background Tasks.
- How to run celery as a daemon? is a short post with the minimal code for running the Celery daemon and Celerybeat as system services on Linux.
- Celery in Production on the Caktus Group blog contains good practices from their experience using Celery with RabbitMQ, monitoring tools and other aspects not often discussed in existing documentation.
- Three quick tips from two years with Celery provides some solid advice on retry delays, the -Ofair flag and global task timeouts for Celery.