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?
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))
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With