芹菜,动态添加定期任务

时间:2019-12-11 14:16:13

标签: celery

https://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html#entries提到add_periodic_task

我没有得到test.s and test.s('hello'),而不仅仅是test('hello')

from celery import Celery
from celery.schedules import crontab

app = Celery()

@app.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
    # Calls test('hello') every 10 seconds.
    sender.add_periodic_task(10.0, test.s('hello'), name='add every 10')

    # Calls test('world') every 30 seconds
    sender.add_periodic_task(30.0, test.s('world'), expires=10)

    # Executes every Monday morning at 7:30 a.m.
    sender.add_periodic_task(
        crontab(hour=7, minute=30, day_of_week=1),
        test.s('Happy Mondays!'),
    )

@app.task
def test(arg):
    print(arg)

sender是什么?我想在add_periodic_task之外打电话给@app.on_after_configure.connect

1 个答案:

答案 0 :(得分:1)

.s()是任务签名-可以将其视为运行任务的占位符-test('hello')会立即调用您的任务,当您只想指示Celery调用时,这不是您想要的定期在setup_periodic_tasks中完成任务。