开发者

Doing something *after* handling a request in Google App Engine

I would like the GAE to do something else once my app has sent a response.

The handler would look like this:

class FooHandler(webapp.RequestHandler):
    def post(self):
        self.response.out.write('Bar')
        send_response()     # this is where I need help!
        do_something_else() # at this point, the response should have been sent

In case you wonder why I try to do this:

I need thread-like behaviour, which is not allowed by GAE's sandboxed environment. So, a function sends several requests whithout caring about th开发者_StackOverflowe response. Each request starts a time-consuming operation (fetching resources) and saves the result into the datastore, where it can be used by the first function.

Note: The request handler has to send a response. If you do not provide any, it will wait for the post function to complete and then return default headers (which is not the behaviour I'm looking for, of course)

If that can help, the solution might be to use a custom wsgi middeleware, but I have no idea how it works (yet)...


Maybe you can use the Task Queues.


As already mentioned, you can use task queues or the deferred API. Another option is outlined by Rafe Kaplan towards the end of his section in this talk here: you can do an asynchronous API call with a result hook function to process the result, and the result hook will be called when the call finishes, after the response is returned to the user!


Presuming you have access to the WSGI layer, you can wrap the WSGI application and provide a callback to be executed once response sent. For how to do this see:

http://code.google.com/p/modwsgi/wiki/RegisteringCleanupCode

Although that is from the mod_wsgi documentation, the cleanup at end of request example should work for any WSGI compliant stack.


You can't GAE sends it's response when the RequestHandler returns if you actually need Threads you will need to write your web application at another hosting company.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜