Celery Periodic Tasks: From Installation to Infinity

This is a quick example of how I got Periodic Tasks to work using Celery without Django. There are lots of examples out there for creating tasks and loose documentation on how to start Celery and Celery Beat, but most of them involve Django. I just wanted to run a simple example and I spent way too long trying to fill in the gaps to get even this simple task to run periodically. So hopefully this quick example will help somebody else out there save some time.

Pre-requisites

Installing Celery

sudo pip install Celery
You'll want to choose your broker; RabbitMQ is what comes recommended by default.

Create a celeryconfig.py

Once you install your broker of choice you can setup the configuration options in celeryconfig.py. In my case I was using MySQL. Here is an example of what my file looked like.

# Change this to your settings
BROKER_URL = 'sqla+mysql://user:password@localhost/database'
CELERY_RESULT_BACKEND = "database"
CELERY_RESULT_DBURI = 'mysql://user:password@localhost/database'

Create some tasks

Create your tasks (generally they will be saved into a file called tasks.py). This is what my tasks.py looks like:

from celery import Celery
 
celery = Celery('tasks')
celery.config_from_object('celeryconfig')
 
@celery.task
def add(x, y):
    return x + y

Update your celeryconfig.py

Now in order for Celery to know how often you want to run your periodic tasks, update your celeryconfig.py to add the following.

from celery.schedules import crontab
 
CELERYBEAT_SCHEDULE = {
    'every-minute': {
        'task': 'tasks.add',
        'schedule': crontab(minute='*/1'),
        'args': (1,2),
    },
}

Start Celery

Now we just need to start a Celery worker with the --beat flag and our task will run every minute. This part was not so obvious, but the --beat flag needs to appear after worker, otherwise nothing will happen.

$ celery -A tasks worker --loglevel=info --beat

Wait a minute...

You should see Celery output a bunch of info as tasks are ran. Among them you should see a line that looks like this:

[2012-12-07 13:26:00,343: INFO/MainProcess] Task tasks.add[44477955-906c-4827-bda2-f6f19822c652] succeeded in 0.00760388374329s: 3

Comments

Awesome !!

I was needing exactly this, and I 'm using Flask. I was doing a very simple test, an implementation, and I spent the past 4 hours trying to understand why it wasn't working at all !!. Thanks a lot !

Really helpful

I was searcing for the same kind of tutorial. Helped me a lot.

Aha!

Was running a separate process for beats, didn't realise I could just do "--beat". Cheers

Awesome tutorial dude! Quick

Awesome tutorial dude! Quick question though, how do I get the RESULT of my scheduled task out of celery.

Previously I'd been sending tasks to celery using the '.delay()' function. Lets say I had a task called "add" in my tasks.py file.

I'd do:

from tasks import add
 
result = add.delay(6,7)
result.get()
>>13

how do I get beats to tell me what the result of it's periodic task is?

Add new comment