【问题标题】:How to set periodic task on Django using celery?如何使用 celery 在 Django 上设置定期任务?
【发布时间】:2019-01-15 14:21:22
【问题描述】:

我当前的代码:

from celery.task.schedules import crontab
from celery.decorators import task, periodic_task


@periodic_task(run_every=crontab(hour=15, minute=55, day_of_week="wed"))
def demo():
    print("testing------------------------")

设置文件:

CELERY_BROKER_URL = os.environ.get('CELERY_BROKER_URL', 'amqp://guest:guest@localhost//')

它不工作,我错过了什么吗?

提前致谢

【问题讨论】:

  • 你运行 celerybeat 了吗?
  • 是的,我在并行终端中运行过
  • 你有日志吗?
  • 此任务没有日志
  • 我在设置上遗漏了什么吗?

标签: python django python-3.x celery django-celery


【解决方案1】:

我认为你应该使用下面的代码,它应该适合你。

celery_app = celery("project_name")

@celery_app.on_after_finalize.connect
def setup_periodic_tasks(sender, **kwargs):
  sender.add_periodic_task(5.0, demo.s(args))

@celery_app.task(bind=True)
def demo(args):
   print("testing------------------------")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-09
    • 2012-05-09
    • 2014-02-23
    • 2021-06-15
    • 1970-01-01
    • 2012-11-18
    • 2017-01-06
    • 2012-12-20
    相关资源
    最近更新 更多