
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.
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:
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.
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
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 !
Fri, 11/08/2013 - 06:44
I was searcing for the same kind of tutorial. Helped me a lot.
Tue, 12/03/2013 - 15:58
Hey Jonathan,
Thanks so much for your post. Simple and very helpful!
Cheers,
Soo Ling
Thu, 05/22/2014 - 21:13
Was running a separate process for beats, didn't realise I could just do "--beat". Cheers
Tue, 06/10/2014 - 05:52
thanks for your help
Thu, 09/18/2014 - 11:31
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?
Thu, 09/25/2014 - 10:29
Can I run beat periodic task scheduler with the -c:
celery worker -Q celery --beat -l info -c 4
?
Wed, 01/07/2015 - 22:30
Very helpful man thanks a lot!
Mon, 08/20/2018 - 07:44
In the below given code, what is "every-minute" means? we are mentioning 'crontab(minute='*/1')' to run the task for every 1 minute(If not , please correct me). So what is the use of 'every-minute' then ?
CELERYBEAT_SCHEDULE = {
'every-minute': {
'task': 'tasks.add',
'schedule': crontab(minute='*/1'),
'args': (1,2),
},
}
Thu, 01/03/2019 - 06:41
every-minute is just an title for the scheduled task
Mon, 05/06/2013 - 01:45