简体   繁体   English

芹菜-在其他任务结束时安排定期任务

[英]Celery - Schedule periodic task at the end of another task

I want to schedule a periodic task with Celery dynamically at the end of another group of task. 我想在另一组任务结束时动态安排Celery的定期任务。

I know how to create (static) periodic tasks with Celery: 我知道如何使用Celery创建(静态)定期任务:

CELERYBEAT_SCHEDULE = {
      'poll_actions': {
          'task': 'tasks.poll_actions',
          'schedule': timedelta(seconds=5)
      }
}

But I want to create periodic jobs dynamically from my tasks (and maybe have a way to stop those periodic jobs when some condition is achieved (all tasks done). 但是我想根据自己的任务动态创建定期作业(也许有一种方法可以在某些条件实现时(所有任务都已完成)停止这些定期作业。

Something like: 就像是:

@celery.task
def run(ids):
    group(prepare.s(id) for id in ids) | execute.s(ids) | poll.s(ids, schedule=timedelta(seconds=5))

@celery.task
def prepare(id):
    ...

@celery.task
def execute(id):
    ...

@celery.task
def poll(ids):
    # This task has to be schedulable on demand
    ...

The straightforward solution to this requires that you be able to add/remove beat scheduler entries on the fly. 一个简单的解决方案要求您能够即时添加/删除节拍调度程序条目。 As of the answering of this question... 截至该问题的答案...

How to dynamically add / remove periodic tasks to Celery (celerybeat) 如何向Celery动态添加/删除定期任务(celerybeat)

This was not possible. 这是不可能的。 I doubt it has become available in the interim because ... 我怀疑它是否可以在过渡时期使用,因为...

You are conflating two concepts here. 您在这里混淆了两个概念。 The notion of "Event Driven Work" and the idea of "Batch Schedule Driven Work"( which is really just the first case where the event happens on a schedule ). “事件驱动工作”的概念和“批处理进度驱动的工作”的想法( 实际上只是事件按计划发生的第一种情况 )。 If you really consider what you are doing here you'll find that there is a rather complex set of edge cases. 如果您真正考虑在这里做什么,将会发现存在相当复杂的边缘情况。 Messages are distributed in nature what happens when groups spawned from different messages start creating conflicting entries? 从本质上讲,消息是分布式的,当从不同消息派生的组开始创建冲突条目时会发生什么? What do you do when you find yourself under a mountain of previously scheduled kruft? 当您发现自己在预先安排的克鲁夫山下时会做什么?

When working with messaging systems you are really looking to build recursive trees. 使用消息传递系统时,您实际上是在构建递归树。 Spindles of work that do something and spawn more messages to do more things. 做某事并产生更多消息以做更多事情的工作主轴。 Cycles(intended or otherwise) aside these ultimately achieve their base cases and terminate. 除了这些周期外,这些周期最终会达到其基本情况并终止。

The answer to whatever you are actually trying to achieve lies with re-encoding your problem within the limitations of your messaging system and asynchronous work framework. 实际要实现的目标的答案就在于在消息传递系统和异步工作框架的限制范围内重新编码问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM