倒计时的芹菜任务 [英] Celery Task with countdown

查看:68
本文介绍了倒计时的芹菜任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Celery 2.5.1,并且尝试使用 countdown 在20秒后运行任务,但该任务会立即执行.

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.

我将其用作:

DemoTask.apply_async(countdown = 20)

我在这里想念东西吗?

推荐答案

问题可能不在正确的时区.通过设置 countdown = 20 ,您可能会告诉Celery在3小时前20秒执行任务.

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.

我建议使用 pytz库告诉Celery在正确的时间启动任务:从日期时间导入日期时间

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

如果您使用的是Django(并且在 settings.py 中设置了 TIME_ZONE ),甚至会更加轻松:

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

这篇关于倒计时的芹菜任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆