java - Report back progress of long request on Google Appengine -
i'm working on implementing web application, google appengine backend, on expected behaviour follows:
- user selects couple of parameters complex analysis
- user presses 'start'
- an empty 'response' page returned user, processing continues
- the analysis somehow continues on server , partial results being computed shown / added in response page.
i'm expecting total computation around 30-40 seconds (so way under 60 seconds required appengine).
steps 1 , 2 trivial. know step 4 somehow completed using step ajax, i'm not sure how implement step 3.
thanks!
you can use task queue , datastore. need 3 handlers:
- the task handler, doing hard work. store progress in datastore.
- a handler starts task in background , returns 'blank' page
- a handler status
note: page cannot blank. must have javascript on checks status. think true channel api
too.
anyway heres code in python:
class longtaskstatus(ndb.model): is_complete = ndb.booleanproperty() percentage = ndb.floatproperty() messages = ndb.stringproperty(repeated=true) class longtaskhandler(webapp2.requesthandler): def get(self): # query existing status model or create new 1 # work ... # update progress status = longtaskstatus() status.messages.appen('still busy...') status.put() # work ... class starthandler(webapp2.requesthandler): def get(self): # start task taskqueue.add(url='/longtask') # return page uses javascript check progress every few seconds template = jinja_environment.get_template('taskprogress.html') self.response.write(template.render(template_values)) class checktaskstatus(wenapp2.requesthandler): def get(self): query = longtaskstatus.query().fetch(1) result = {} if query: status = query[0] result = { 'is_complete': status.is_complete, 'percentage': status.percentage, 'messages': status.messages } self.response.write(json.dumps(result))
and heres "blank" page:
<!doctype html> <html lang="en"> <body> <div id="status"></div> <script> window.setinterval(function(){ $.get( "ajax/test.html", function( data ) { $( ".status" ).html( data ); }); }, 5000); </script> </body> </html>
edit: other option without task queue
if have unique way of identifying task before started potentially speed method not using task queue api.
heres how:
- call longtaskhandler via javascript
- redirect loading page, calls checktaskstatus.
this should faster using task queues, unfortunately need way identify task before started. eg userid, session etc
Comments
Post a Comment