Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use the task queue and the datastore. You need 3 handlers:</p> <ol> <li>The task handler, doing the hard work. It will store its progress in the datastore.</li> <li>A handler that starts the task in the background and returns the 'blank' page</li> <li>A handler to get the status</li> </ol> <p>Note: The page cannot be really blank. It must have javascript on it that checks the status. I think this is true with the <code>Channel API</code> too.</p> <p>Anyway heres the code in Python:</p> <pre><code>class LongTaskStatus(ndb.Model): is_complete = ndb.BooleanProperty() percentage = ndb.FloatProperty() messages = ndb.StringProperty(repeated=True) class LongTaskHandler(webapp2.RequestHandler): def get(self): # Query for existing status model or create a new one # Does work ... # Update progress status = LongTaskStatus() status.messages.appen('Still busy...') status.put() # Does work ... class StartHandler(webapp2.RequestHandler): def get(self): # start the task taskqueue.add(url='/longtask') # Return a page which uses javascript to check the 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)) </code></pre> <p>and heres the "blank" page:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html lang="en"&gt; &lt;body&gt; &lt;div id="status"&gt;&lt;/div&gt; &lt;script&gt; window.setInterval(function(){ $.get( "ajax/test.html", function( data ) { $( ".status" ).html( data ); }); }, 5000); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>Edit: Other option without Task Queue</strong></p> <p>If you have a unique way of identifying the task <strong>before it is started</strong> you could potentially speed this method up by not using the task queue api.</p> <p>Heres how:</p> <ol> <li>Call the LongTaskHandler via javascript</li> <li>Redirect to a loading page, which calls CheckTaskStatus.</li> </ol> <p>This should be faster than using task queues, but unfortunately you need a way to identify the task before it is started. eg userid, session etc</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload