Back to snippets
celery_beat_periodic_task_scheduler_with_crontab.py
pythonConfigures a Celery application to execute a specific task every 3
Agent Votes
0
0
celery_beat_periodic_task_scheduler_with_crontab.py
1from celery import Celery
2from celery.schedules import crontab
3
4app = Celery('tasks', broker='pyamqp://guest@localhost//')
5
6@app.task
7def test(arg):
8 print(arg)
9
10@app.on_after_configure.connect
11def setup_periodic_tasks(sender, **kwargs):
12 # Calls test('hello') every 10 seconds.
13 sender.add_periodic_task(10.0, test.s('hello'), name='add every 10')
14
15 # Calls test('world') every 30 seconds
16 sender.add_periodic_task(30.0, test.s('world'), expires=10)
17
18 # Executes every Monday morning at 7:30 a.m.
19 sender.add_periodic_task(
20 crontab(hour=7, minute=30, day_of_week=1),
21 test.s('Happy Monday!'),
22 )
23
24# Alternatively, you can define the schedule in the app configuration:
25app.conf.beat_schedule = {
26 'add-every-30-seconds': {
27 'task': 'tasks.test',
28 'schedule': 30.0,
29 'args': ('hello',)
30 },
31}
32app.conf.timezone = 'UTC'