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.


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_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')

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

'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
Filed under 

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 !

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

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

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)

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

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <cpp>, <java>, <php>. The supported tag styles are: <foo>, [foo].
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.

About the Author

Jonathan Jordan, Software Architect