芹菜节拍-每个任务的时区不同 [英] Celery beat - different time zone per task

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

问题描述

我正在用芹菜拍子安排一些任务。我可以使用CELERY_TIMEZONE设置通过crontab时间表来安排任务,它在提到的时区中的预定时间运行。



但是我想成为能够在同一应用程序中针对不同时区设置多个此类任务(单个django settings.py)。我知道计划任务时哪个任务需要在哪个时区运行。



是否可以为每个任务指定不同的时区?



我正在使用Django(1.4)和芹菜(3.0.11)和django芹菜(3.0.11)。



我查看了 djcelery.schedulers.DatabaseScheduler 类和它的基类,但是我无法弄清楚时区的使用方式和位置。我可以编写一个自定义的调度程序来使每个作业在不同的时区运行吗?



谢谢

解决方案

您可以在celery计划中实现单个任务的时区感知计划。这样,您可以为每个芹菜时间表指定一个单独的立即功能 ,从而根据特定时区的当地时间(也可以调整为例如夏时制)运行任务。

crontab 支持 nowfun 参数以指定用于检查其是否应运行的datetime函数

 导入日期时间
导入pytz
nowfun = lambda:datetime.datetime.now(pytz.timezone('Europe / Berlin'))

在您的计划中,可以通过以下方式将此函数设置为datetime函数:

 'periodic_task':{
'task':'api.tasks.periodic',
'schedule':crontab(hour = 6,minutes = 30, nowfun = nowfun)
}

每天运行于欧洲中部时间6.30,调整为夏令时



如果您使用more t函数一次,可以考虑创建一个帮助器

 从functools导入部分
cet_crontab =部分(crontab,nowfun = nowfun)
'periodic_task':{
'task':'api.tasks.periodic',
'schedule':cet_crontab(hour = 6,minutes = 30)
}

确保已设置 CELERY_ENABLE_UTC = False ,否则为芹菜将您的时间表转换为UTC。


I am using celery beat to schedule some tasks. I'm able to use the CELERY_TIMEZONE setting to schedule the tasks using the crontab schedule and it runs at the scheduled time in the mentioned time zone.

But I want to be able to setup multiple such tasks for different timezones in the same application (single django settings.py). I know which task needs to run in what timezone when the task is being scheduled.

Is it possible to specify a different timezone for each of the tasks?

I'm using django (1.4) with celery (3.0.11) and django celery (3.0.11).

I've looked at the djcelery.schedulers.DatabaseScheduler class and it's base class, but I can't figure out how and where the timezone is getting used. Can I write a custom scheduler that can make each job run in a different timezone?

Thanks,

解决方案

You can achieve a timezone-aware scheduling of individual tasks in a celery schedule. This way you can run a task according to the local time in a specific timezone (also adjusting to e.g. daylight saving time) by specifying a separate now function for each celery schedule

crontab supports the nowfun argument to specify the datetime function to be used to check if it should run

import datetime
import pytz
nowfun = lambda: datetime.datetime.now(pytz.timezone('Europe/Berlin'))

In your schedule, set this function as the datetime function via

'periodic_task': {
    'task': 'api.tasks.periodic',
    'schedule': crontab(hour=6, minute=30, nowfun=nowfun)
}

This runs every day at 6.30am CET adjusted to daylight savings.

In case you use the function more than once, consider creating a helper

from functools import partial
cet_crontab = partial(crontab, nowfun=nowfun)
'periodic_task': {
    'task': 'api.tasks.periodic',
    'schedule': cet_crontab(hour=6, minute=30)
}

Make sure you have CELERY_ENABLE_UTC = False set, otherwise celery converts your schedules to UTC.

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

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