使用烧瓶任务进行芹菜节拍

问题描述 投票:0回答:1

我想使用Celery在Flask中执行cron作业,但是关于芹菜节拍时间表,我遇到了一个问题,因为似乎我的任务尚未加载,并且我不知道如何检查问题所在。

这是我在views.py中定义Flask应用的地方:

from celery.schedules import crontab

app = Flask(__name__)
app.config.update(
    CELERY_BROKER_URL='redis://localhost:6379',
    CELERY_RESULT_BACKEND='redis://localhost:6379',
    CELERY_BEAT_SCHEDULE={
        'task-number-one': {
            'task': 'app.tasks.test',
            'schedule': crontab(minute="*"),
        }
    },
    CELERY_IMPORTS = ('app.tasks'),
    CELERY_TASK_RESULT_EXPIRES = 30,
    CELERY_TIMEZONE = 'UTC',
)

以及在tasks.py中创建我的芹菜对象的位置:

from celery import Celery
from app.views import app
from celery import shared_task


def make_celery(app):
    celery = Celery(app.import_name, backend=app.config['CELERY_RESULT_BACKEND'],
                    broker=app.config['CELERY_BROKER_URL'])
    celery.conf.update(app.config)
    TaskBase = celery.Task
    class ContextTask(TaskBase):
        abstract = True
        def __call__(self, *args, **kwargs):
            with app.app_context():
                return TaskBase.__call__(self, *args, **kwargs)
    celery.Task = ContextTask
    return celery

celery_app = make_celery(app)

@celery_app.task()
def test():
    logger = test.get_logger()
    logger.info("Hello")

views.py和task.py在同一目录下,称为app

这是我开办芹菜工作者的东西(这里一切似乎都很正常):

但是这是我启动芹菜节拍时所拥有的,似乎我的任务从未按计划发送,但我不知道在哪里检查:enter image description here

您能帮我吗?最佳

flask celery celerybeat
1个回答
0
投票

我相信至少在发送@app.on_after_configure.connect信号之后,才需要配置Celery-Beat任务。您应该可以在task.py文件中执行以下操作。

celery_app.conf.CELERYBEAT_SCHEDULE = {
    "test-every-minue": {
    "task": "tasks.test",
    "schedule": crontab(minute="*"),
     },
}

或者,如果您的任务与celery应用程序实例在同一文件中定义,则可以使用此装饰器语法。

@celery_app.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
    sender.add_periodic_task(5 , test_two.s(), name='test-every-5')

如果您的任务是在单独的模块中定义的,则可以在导入任务后使用@app.on_after_finalize.connect装饰器。

@app.on_after_finalize.connect
def setup_periodic_tasks(sender, **kwargs):
    from app.tasks import test
    sender.add_periodic_task(10.0, test.s(), name='test-every-10')

Celery Beat Entry Docs

© www.soinside.com 2019 - 2024. All rights reserved.