芹菜时间表,现在不能泡菜

Celery Schedule, can't pickle nowfun

我正在尝试将 Celery 配置为 运行 在不同时区执行不同任务以保持夏令时的变化。我这样设置我的任务:

import datetime
import pytz
from celery import Celery
from celery.schedules import crontab

app = Celery('my_project')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

NOW_MT = lambda: datetime.datetime.now(pytz.timezone('America/Denver'))

app.conf.beat_schedule = {
    "My Daily Mountain Time Task": {
        "task": "app.tasks.some_task",
        "schedule": crontab(minute='0', hour='0', nowfun=NOW_MT)
    },
    "My Daily UTC Task": {
        "task": "app.tasks.some_other_task",
        "schedule": crontab(minute='0', hour='0')
    }
}

但我的 celery beat 过程出错并显示以下消息:

Can't pickle <function <lambda> at 0x7f71055a4670>: attribute lookup <lambda> on app.celery failed

使用函数代替 lambda 函数

import datetime
import pytz
from celery import Celery
from celery.schedules import crontab

app = Celery('my_project')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

def NOW_MT():
    return datetime.datetime.now(pytz.timezone('America/Denver'))

app.conf.beat_schedule = {
    "My Daily Mountain Time Task": {
        "task": "app.tasks.some_task",
        "schedule": crontab(minute='0', hour='0', nowfun=NOW_MT)
    },
    "My Daily UTC Task": {
        "task": "app.tasks.some_other_task",
        "schedule": crontab(minute='0', hour='0')
    }
}