Processing data after returning content - GAE Python
Question: I want to collect some statistical data which needs some time to process, but
it is not going affect user content which is returning. I am currently doing
it by first caching, then processing it with a cron job.

Is there any way to do this job immediately after returning user content?

It should be like:

    
    
    def post(self):
        self.response.out.write("some output")
        # here user should not wait any more output
        # loading should be end in user browser.
    
        collectSomeStatistics(self)
    

(I am using Python 2.7)


Answer: You can use the [deferred
library](https://developers.google.com/appengine/articles/deferred)

    
    
    from google.appengine.ext import deferred
    
    def do_something_expensive(a, b, c=None):
      logging.info("Doing something expensive!")
      # Do your work here
    
    # Somewhere else
    deferred.defer(do_something_expensive, "Hello, world!", 42, c=True)
    

So your code becomes:

    
    
    def post(self):
      deferred.defer(collectSomeStatistics,args)
      self.response.out.write("some output")
    

This then executes your function as a separate task, and the call to deferred
returns immediately. Of course you won't be able to include any results from
that deferred function in the content you return to the user.

