views:

109

answers:

2

Is there a way in Web.py to detect and handle connection being closed by user while the request is processing?

I tried setting unloadhook handler, but it doesn't get called in this case. It's only called after the request was successfully completed:


class handler:
    def __init__(self):
        pass

    def GET(self):
        try:
            while(True):
                pass
        except Exception, e:
            logger.debug(e)

def unload():
    logger.debug('unloaded')

try:
    app = web.application(urls, globals())
    app.add_processor(web.unloadhook(unload))
    application = app.wsgifunc()

except Exception, e:
    logger.debug(e)

I open the app in a browser and when it starts spinning in the while loop I break the request, but no exception is thrown.

+1  A: 

Set a timeout, and catch an exception type that specifically covers the case of a closed connection.

See this on exception handling in Python

Chris Ballance
Thanks, but it doesn't seem to be working (see my updated question above)
Leonid
I don't see where you set a timeout.
Chris Ballance
I've set Timeout in httpd.confNot sure if you meant it to be set in webpy, but if that's the case I couldn't find how to make it.
Leonid
+1  A: 

Due to the way that web servers and WSGI itself works, generally there is no reliable way of doing it. For an explanation read:

http://groups.google.com/group/modwsgi/browse_frm/thread/8ebd9aca9d317ac9

Graham Dumpleton