Last active
March 23, 2021 05:10
-
-
Save ekinertac/ff8b76f0b43c5022474e608822395fef to your computer and use it in GitHub Desktop.
Django simple background tas runner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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