简体   繁体   中英

How to return HttpResponse before script finishes?

I'm currently writing a web application with django and now put in a button on my main page that will open a new browser window and run a python script. Now the script takes about 5 min to finish. How can I return a Response before that time, so the user knows something is happening and it just takes some time. Here's the part of the views.py im using.

Thank you for your help!

def redoScan(request):
   main()
   return HttpResponse("Submitted new scan job. New data will be displayed on main page in 5 minutes")

You may return HttpResponse and add asynchronously task. Use celary or cron or some other task manager

You can use a message queue such as django-celery. As you can tell it already has a simple django integration. Celery lets you asyncronously pass 'tasks' to the message queue to be processed. This would allow a user to begin time intensive process' without having to wait for them.

http://mathematism.com/2010/02/16/message-queues-django-and-celery-quick-start/

As suggested by pahaz a cron could be used to aggregate data too. If you are doing the same computations regularly for every user. Then this could easily be converted to a custom management command and the data could be stored in an itermediary table. That way the user would just have to look up the data and not have to wait for the data to be processed.

Alright, I have actually used Javascript now to write a status on the screen and then I call an AJAX function that will post the new status once the scan is done. That wasn't actually to bad and since I'm using AJAX anyway not a big deal. Should have thought of that before.

Thank you guys anyways for your help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM