File: views.py

package info (click to toggle)
django-q 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,276 kB
  • sloc: python: 5,686; makefile: 181; sh: 30
file content (31 lines) | stat: -rw-r--r-- 753 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import time

from django.http import HttpResponse
from django.urls import reverse

from django_q import tasks


# internal function to be called with django_q
def new_task(run_for_minutes):
    print("Task started")
    time.sleep(run_for_minutes)
    print("Task done")
    return True


def add_task(request):
    task_id = tasks.async_task(new_task, 5)
    result_url = reverse("get_result", args=[task_id])
    return HttpResponse(
        f"Added async task with <a href='{result_url}'>Go to results</a>"
    )


def get_result(request, task_id):
    task = tasks.fetch(task_id)
    if not task:
        msg = "Task running... please refresh after some time"
    else:
        msg = f"Async task result: {task.result}"
    return HttpResponse(msg)