HttpResponse update every few seconds

My application in Django can create large SQL queries very much . I am currently using an HttpRequest object for the data I need, and then an HttpResponse to return what I want to show the user.

Obviously, I can let the user wait a minute while these many sets of queries are executed and retrieved from the database, and then return this monolithic HTML page.

Ideally, I would like to refresh the page whenever I want, something like:

 For i,e in enumerate(example): Table.objects.filter(someObjectForFilter[i]). #Return the object to the page. #Then Loop again, 'updating' the response after each iteration. 

Is it possible?

0
source share
2 answers

I recently discovered that HttpResponse can be a generator:

 def myview(request, params): return HttpResponse(mygenerator(params)) def mygenerator(params): for i,e in enumerate(params): yield '<li>%s</li>' % Table.objects.filter(someObjectForFilter[i]) 

This will gradually return the results of mygenerator to the page, wrapped in HTML <li> for display.

+4
source

Your approach is a bit flawed. You have several different options.

The first, probably the easiest, is to use AJAX and HTTPRequest. Have a series of them, each of which leads to one Table.objects.filter(someObjectForFilter[i]). . Upon completion of each of them, the script completes and returns the results to the client. The client updates the user interface and initiates the next request with another AJAX call.

Another method is to use a batch system. This is a bit heavier, but probably a better design if you go for a real "heavy lift" in the database. You will need to run a batch daemon (this requires a simple cron probe) to scan incoming tasks. The user wants to accomplish something, so their request sends this task (it could just be a row in the database with their parameters). The daemon captures it, processes it completely autonomously - perhaps even on another machine - and updates the task line when it ends with the results. The client can then be updated periodically to check the status of this line using traditional or AJAX methods.

+2
source

All Articles