Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Celery Task with countdown

Tags:

python

celery

I am using Celery 2.5.1 and I am trying to use countdown to run the task after 20 seconds, but it gets executed immediately.

I am using it as:

DemoTask.apply_async(countdown = 20)

Am I missing something here?

like image 898
Siddharth Avatar asked Jul 07 '14 14:07

Siddharth


2 Answers

The problem is likely not being in the right timezone. By setting countdown=20 you might be telling Celery to execute the task 20 seconds after 3 hours ago.

I suggest using the pytz library to tell Celery to start the task at the right time:

from datetime import datetime, timedelta
from pytz import timezone

# Set timezone: http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
my_tz = timezone('US/Eastern')

DemoTask.apply_async(eta=my_tz.localize(datetime.now()) + timedelta(seconds=20))

Or even easier if you are using Django (and have set TIME_ZONE in settings.py):

from datetime import timedelta
from django.utils.timezone import now

DemoTask.apply_async(eta=now() + timedelta(seconds=20))
like image 198
dwitvliet Avatar answered Sep 22 '22 18:09

dwitvliet


In addition for the timezone problem laid out by @Banana, make sure that the celery configuration option always_eager, which makes celery skip is set to False - otherwise, Celery ignores the countdown and the eta.

like image 25
phihag Avatar answered Sep 21 '22 18:09

phihag