Según el WSGI PEP, si su aplicación devuelve un iterador con un método close(), el servidor debería llamar al final de la solicitud. He aquí un ejemplo:
"""
Run this script with 'python sleepy_app.py'. Then try connecting to the server
with curl:
curl -N http://localhost:8000/
You should see a counter printed in your terminal, incrementing once every
second.
Hit Ctrl-C on the curl window to disconnect the client. Then watch the
server's output. If running with a WSGI-compliant server, you should see
"SLEEPY CONNECTION CLOSE" printed to the terminal.
"""
class SleepyApp(object):
def __init__(self, environ, start_response):
self.environ = environ
self.start_response = start_response
def __iter__(self):
self.start_response('200 OK', [('Content-type', 'text/plain')])
# print out one number every 10 seconds.
import time # imported late for easier gevent patching
counter = 0
while True:
print "SLEEPY", counter
yield str(counter) + '\n'
counter += 1
time.sleep(1)
def close(self):
print "SLEEPY CONNECTION CLOSE"
def run_gevent():
from gevent.monkey import patch_all
patch_all()
from gevent.pywsgi import WSGIServer
server = WSGIServer(('0.0.0.0', 8000), SleepyApp)
print "Server running on port 0.0.0.0:8000. Ctrl+C to quit"
server.serve_forever()
if __name__ == '__main__':
run_gevent()
Sin embargo, hay a bug en aplicación wsgiref de Python (y en el servidor dev Django que herede de ella) que impide cerrar() de ser llamado en cliente se desconecta a mitad de camino. Así que evite wsgiref y el servidor de desarrollo Django para este caso.
Tenga en cuenta también que close() no se disparará inmediatamente cuando el cliente se desconecta. Sucederá cuando trates de escribir algún mensaje al cliente y falle porque la conexión ya no está allí.
Muchas gracias por su pensamiento, realmente lo aprecio. ¡Esto es algo que REALMENTE me encantaría saber! =) #gevent en freenode parece bastante silencioso en estos días ... Gracias por tu respuesta denis! – RadiantHex
Me pregunto, ¿sería una idea terrible presentar alguna excepción en la aplicación WSGI de forma asincrónica si un cliente se desconecta? –