django芹菜:如何设置任务以编程方式以特定间隔运行 [英] django celery: how to set task to run at specific interval programmatically

查看:109
本文介绍了django芹菜:如何设置任务以编程方式以特定间隔运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现我可以将任务设置为在特定时间内从这里,但这只是在任务声明中完成。如何设置任务定期动态运行?

I found that I can set the task to run at specific interval at specific times from here, but that was only done during task declaration. How do I set a task to run periodically dynamically?

推荐答案

计划是从设置派生,因此在运行时似乎是不可变的。

The schedule is derived from a setting, and thus seems to be immutable at runtime.

你可以完成你的任务'使用任务ETA 查找。这保证您的任务不会在所需时间之前运行,但不承诺在指定的时间运行任务 - 如果工作人员在指定的ETA超载,则任务可能稍后运行

You can probably accomplish what you're looking for using Task ETAs. This guarantees that your task won't run before the desired time, but doesn't promise to run the task at the designated time—if the workers are overloaded at the designated ETA, the task may run later.

如果该限制不是问题,您可以编写一个首先运行的任务,如:

If that restriction isn't an issue, you could write a task which would first run itself like:

@task
def mytask():
    keep_running = # Boolean, should the task keep running?
    if keep_running:
        run_again = # calculate when to run again
        mytask.apply_async(eta=run_again)
    # ... do the stuff you came here to do ...

这种方法的主要缺点是您依靠任务库记住飞行中的任务。如果其中一个在关闭下一个之前失败,则任务将永远不会再运行。如果您的经纪人不坚持到磁盘,并且死亡(使用它执行所有的飞行任务),那么这些任务都不会再运行。

The major downside of this approach is that you are relying on the taskstore to remember the tasks in flight. If one of them fails before firing off the next one, then the task will never run again. If your broker isn't persisted to disk and it dies (taking all in-flight tasks with it), then none of those tasks will run again.

你可以解决这些问题与某种交易记录和定期的保姆任务,他们的工作是找到这样的重复任务,死亡不幸死亡,并恢复他们。

You could solve these issues with some kind of transaction logging and a periodic "nanny" task whose job it is to find such repeating tasks that died an untimely death and revive them.

如果我不得不实施你所描述的,我认为这是我会如何处理的。

If I had to implement what you've described, I think this is how I would approach it.

这篇关于django芹菜:如何设置任务以编程方式以特定间隔运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆