Skip to main content
 首页 » 编程设计

python之如何使用 django-background-tasks 启动后台周期性任务

2024年11月01日31dflying

我想为我的 SQLite 文件中的检查值创建定期任务。我尝试使用 django-background-tasks 创建重复任务,但它不起作用。当我在 cmd 中使用 python manage.py process_tasks 时它工作正常。如何使用 start django 启动此任务而不使用此命令?

我在 Windows 10 上开发我的网络应用。

网址.py

from django.contrib import admin 
from django.urls import path, include 
from client import tasks as task 
 
urlpatterns = [ 
    path('admin/', admin.site.urls), 
    path('', include('client.urls')), 
    path('account/', include('account.urls')), 
] 
 
task.check_groups(repeat=10, repeat_until=None) 

任务.py

from background_task import background 
from django.utils import timezone 
from client.models import StudyGroup, Account 
 
@background() 
def check_groups(): 
    check_active_groups() 
    check_inactive_groups() 
 

请您参考如下方法:

来自文档:

setup a cron task (or long running process) to execute the tasks

您必须设置 cron 或其他一些定期运行器来调用该命令,因为 Django 本身无法执行此操作。