Skip to content

Instantly share code, notes, and snippets.

@ekinertac
Last active March 23, 2021 05:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ekinertac/ff8b76f0b43c5022474e608822395fef to your computer and use it in GitHub Desktop.
Save ekinertac/ff8b76f0b43c5022474e608822395fef to your computer and use it in GitHub Desktop.
Django simple background tas runner
import threading
from .models import Crawl
def startCrawl(request):
task = Crawl()
task.save()
t = threading.Thread(target=doCrawl,args=[task.id],daemon=True)
t.start()
return JsonResponse({'id':task.id})
def checkCrawl(request,id):
task = Crawl.objects.get(pk=id)
return JsonResponse({'is_done':task.is_done, result:task.result})
def doCrawl(id):
task = Crawl.objects.get(pk=id)
# Do crawling, etc.
task.result = result
task.is_done = True
task.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment