0
我正在使用Django,我想开始一些后台任务。我找到了“Django后台任务”库。它几乎包含了我需要的所有内容,但我无法找到如何在文档(http://django-background-tasks.readthedocs.io/en/latest/)中的任何位置获取任务(等待/正在运行/已完成)的状态。如果有人能告诉我如何获得任务的状态,这对我非常有帮助。从Django获取任务的状态背景任务
我正在使用Django,我想开始一些后台任务。我找到了“Django后台任务”库。它几乎包含了我需要的所有内容,但我无法找到如何在文档(http://django-background-tasks.readthedocs.io/en/latest/)中的任何位置获取任务(等待/正在运行/已完成)的状态。如果有人能告诉我如何获得任务的状态,这对我非常有帮助。从Django获取任务的状态背景任务
将任务插入数据库表background_task
中,完成后将任务从background_task
表移至background_task_completedtask
表。您可以使用此信息创建视图以获取所有/特定任务的状态。
例:
from background_task.models import Task
from background_task.models_completed import CompletedTask
from datetime import datetime
from django.utils import timezone
def get_status(request):
now = timezone.now()
# pending tasks will have `run_at` column greater than current time.
# Similar for running tasks, it shall be
# greater than or equal to `locked_at` column.
# Running tasks won't work with SQLite DB,
# because of concurrency issues in SQLite.
pending_tasks_qs = Task.objects.filter(run_at__gt=now)
running_tasks_qs = Task.objects.filter(locked_at__gte=now)
# Completed tasks goes in `CompletedTask` model.
# I have picked all, you can choose to filter based on what you want.
completed_tasks_qs = CompletedTask.objects.all()
# main logic here to return this as a response.
# just for test
print (pending_tasks_qs, running_tasks_qs, completed_tasks_qs)
return HttpResponse("ok")
最后,寄存器在urlpatterns的该视图并检查其状态。