Celery Task with countdown

被刻印的时光 ゝ 提交于 2020-12-29 09:51:43

问题


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?


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/24613377/celery-task-with-countdown

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!