What is celery beat and how to use it?

Celery is a widely recognized distributed task queue for pythonic projects. Its sole purpose is to reduce load of web servers by delegating time-consuming tasks to separate processes. Our web servers should handle one request for no longer than a fraction of second, therefore running long tasks synchronously substantially reduces application’s throughput.

So we are able to delegate and queue hard work elsewhere. Obviously, this has to be done explicitly. Sometimes we have to run some tasks periodically.  Let’s say we have a business requirement:

Users pay for access to our service. Payment can be done only for one month. Doing so postpones account’s expiration by 31 days. Expired accounts should be deactivated within 4 hours. Eventually e-mail with apropriate information should be send.

To fulfill such requirement, we would have to scan application’s user list for expired accounts at least every 4 hours. That is where celery beat comes in. Its simply a celery periodic tasks scheduler.

Why should I use celery beat? I have crontab

That’s a strong argument against celery beat. After all, why would anyone care about some less-known solution that is meant to replace battle-tested crond service present in all modern unix distros? However, using crontab requires additional knowledge and usually root access on server. AFAIK, there is no such widespread service in Windows .


First argument in favour of celery beat is its portability. Every environment that can run Python will be also sufficient for celery beat. Taking development and test environments into consideration, this is a serious advantage. Celery beat is just another part of your application, so new version could be easily deployed locally every time codebase changes. This speeds up whole process and makes one headache go away.

No more “glue” scripts

Using celery beat eliminates need for writing little glue scripts with one purpose – run some checks, then eventually sending tasks to regular celery worker. Usually these would be run periodically by crond, therefore crond configuration would effectively tie application to certain run environment.

It is in Python!

An obvious advantage. No administration skills required to have full-fledged periodic tasks scheduler 🙂

How to properly use celery beat in aforementioned scenario?

Let’s break our problem into smaller ones first. We have to:

  • Scan user’s list, looking for expired accounts
  • For every expired account:
    • deactivate it
    • send an e-mail

We know for sure, that first thing should be done periodically, so we’ll be running it directly from celery beat:

1:0 for scheduler.  We might implement whole thing in this task, but this approach is strongly discouraged.  First of all, resultant list of accounts can be large. Therefore, deactivating all accounts and sending e-mails would take very long time. Furthermore, failure in processing one account will affect the whole thing.

Fortunately, we can easily trigger new tasks inside others using apply_async:

We could stop here, but deactivate_account_and_send_email  still can be split into two smaller tasks. However, we can’t just fire both using apply_async because they would run independently and we could end up with sending email of expiration to account that wasn’t deactivated due to some failure 🙂

So we need to link these tasks together somehow. Fortunately, Celery has appropriate mechanism called chains. Chained tasks will be executed sequentially. What is more, next task will get result from previous one as first argument.

Working example:

Whole magic takes place in line 15. chain is created (note absence of explicit argument send_expiration_email! Result of previous task in chain will be passed). Then, in line 16. chain is scheduled to run asynchronously.


Celery once again proves to be powerful tool. Celery beat is a great addition to passive celery worker and saves programmers from learning crond.

Further info:


4 thoughts to “What is celery beat and how to use it?”

  1. Hello! I’m implementing a celery task by starting from a class (APIView) in Django Rest Framework, in particular the get method. The method was originally implemented with the use of external functions to generate a call to a remote SOAP service. The fact is, if I use celery i can execute the task without problem (after having adjusted it with regard to argument passing to the get method internal functions).But, if i use celery beat, the parameters passed to the external “library” function, once the task is called, are strings and not serialized dicts. Any advise on how to pass these parameters to an external function (which of course I’ve imported in the module code). TIA

    1. Hi Maurizio,
      Celery does serialize parameters when they are sent over a broker. With default JSON serialization you will get only strings, floats, bools, dicts and lists. Could you please tell more what are types of arguments you are trying to pass and maybe paste some code to demonstrate the issue?

      Perhaps your best bet is to serialize arguments to desired types from strings inside celery task

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.