通过视图函数设置周期性任务计划(Django app)

Periodic task schedules set through view functions (Django app)

我通过将它们包含在我的 settings.pyCELERYBEAT_SCHEDULE 词典中来为我的 Django 应用安排重复任务。例如:

CELERYBEAT_SCHEDULE = {
    'tasks.rank_photos': {
        'task': 'tasks.rank_photos',
        'schedule': timedelta(seconds=5*60),
    },
    'tasks.trim_whose_online': {
        'task': 'tasks.trim_whose_online',
        'schedule': timedelta(seconds=10*60),
    },
}

这些任务会定期 运行(在应用的整个生命周期内)。

我想知道是否有办法让我的应用程序的普通用户启动定期任务? IE。有没有办法从 views.py 控制这种调度?如果不是,为什么不呢?如果是的话,一个说明性的例子会很棒。提前致谢。

您可以使用 django-celery-beat package. It defines several models (e.g. PeriodicTask) and it will allow you to schedule tasks in your views by simply using those models to create or edit periodic tasks. It is mentioned in the official docs

There’s also the django-celery-beat extension that stores the schedule in the Django database, and presents a convenient admin interface to manage periodic tasks at runtime.