【问题标题】:Celery - Start and stop periodic task from tornado interfaceCelery - 从龙卷风界面启动和停止定期任务
【发布时间】:2019-08-10 11:24:11
【问题描述】:

我正在尝试从龙卷风应用程序界面启动和停止 celery 定期任务。

例如,假设有两个任务:AB。 我希望用户能够从 HTML 表单中选择周期(每分钟、每月、每 5 分钟等),然后在任务 A 上单击 start。 用户可以对任务 B 执行相同的操作。然后返回一个页面,该页面有一个按钮可以随时停止任务 A 和/或任务 B。

我在 stackoverflow 上浏览了很多关于该主题的问题,但没有人回答这个简单的问题。

到目前为止,我的龙卷风应用程序处理简单的芹菜工人没有任何问题,我面临的问题是periodic tasks.(https://docs.celeryproject.org/en/latest/reference/celery.html#celery.Celery.on_after_configure)

continuous_monitoring_worker.py:

continuous_tracking_worker_app.conf.timezone = 'Europe/London'

@continuous_tracking_worker_app.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
    print('OK inside setup periodic tasks ...')
    sender.add_periodic_task(10.0, test.s('Hello World'), name='add every 10')

    # In case I want to stop the task later
    #time.sleep(35)
    #print('beat_schedule = {}'.format(continuous_tracking_worker_app.conf.beat_schedule))
    #del continuous_tracking_worker_app.conf.beat_schedule['add every 10']

@continuous_tracking_worker_app.task
def test(name):
    print('Periodic task called, name = {}'.format(name))

使用这段代码,我遇到了以下问题:当我启动应用程序时,它每 10 秒启动一次任务(我猜是因为 @continuous_tracking_worker_app.on_after_configure.connect 装饰器)。但我希望从界面按需启动任务(通过调用setup_periodic_tasks(continuous_tracking_worker_app) 在我的龙卷风视图文件的后端,而不是在龙卷风应用程序启动时!

【问题讨论】:

  • 顺便说一句,我有 celery Beat 使用以下命令在另一个 shell 上运行:celery -A continuous_worker worker -l info -B

标签: cron celery scheduled-tasks periodic-task


【解决方案1】:

Celery Beat 做不到。

您可以考虑使用使之成为可能的Celery Redbeat

【讨论】:

    【解决方案2】:

    Celery Beat 可以做到这一点。

    只需按照here, (go to the expiration tab) 的描述设置 expires 变量

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多