如何在Django中动态安排任务? [英] How to dynamically schedule tasks in Django?

查看:93
本文介绍了如何在Django中动态安排任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Django中构建一个应用程序,使用户可以在运行时指定的时间每天执行一些任务。

I need to build an app in Django that lets the user do some task everyday at the time they specify at runtime.

我看过Celery,但不能找到任何有帮助的东西。我发现了apply_async,并且可以让任务在指定的时间执行一次,但不能重复执行。我想念什么,但不知道。

I have looked at Celery but couldn't​ find anything that will help. I found apply_async and I can get the task to execute once at the specificied duration but not recurrent. I am missing something but don't know what.

请提出如何实现此目标的建议。

Please suggest how can I achieve this.

推荐答案

django-celery-beat 软件包,可用于动态地将任务添加到数据库,然后按照您在数据库中定义的方式执行它们。(例如,每5分钟执行一次),但是当前它们存在错误,导致将任务添加到数据库时,该任务不会附加到芹菜队列中。建议的解决方法是每次添加新任务时重新启动celery过程。

我用Dan Baders schedule 软件包解决了这个问题。我在每分钟安排任务,检查数据库是否需要在当前时刻执行任务。然后,我在新线程中启动每个任务。

希望这会有所帮助。

There is django-celery-beat package which allows you to dynamicly add tasks to database and then they are executed as you defined in database.(e.g. every 5 minutes) But currently they have bug which causes that task is not appended to celery queue when added to database. One suggested workaround is to restart celery process every time that new task is added.
I solved it with Dan Baders schedule package. I scheduled task on every minute which checks database for tasks that need to be executed in current minute. Then I start each of this tasks in new thread.
Hope this helps.

这篇关于如何在Django中动态安排任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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