¿Cerrar todas las conexiones de enchufe (mantener vivo) en tornado?
Frecuentes
Visto 1,916 equipos
1
I'm running a set of tornado instances that handles many requests from a small set of keep-alive connections. When I take down the server for maintenance I want to gracefully close the keep-alive requests so I can take the server down. Is there a way to tell clients "Hey this socket is closing" with Tornado? I looked around and self.finish()
just flushes the connection.
2 Respuestas
2
Eventually, I hit on a solution for the kind of graceful shutdown I needed (note that this may , solamente work with Tornado 3.2, as it depends on the request having a connection and checking for the no_keep_alive
attribute. Ultimately, there's no need to close a connection unless it's actively sending data (so stray existing connections don't really matter).
- I set up a variable on the application that notes whether the application is shutting down.
- In
initialize()
I check for this and if we are shutting down, I set theConnection: close
header to comply with the RFC and then also setself.request.connection.no_keep_alive = True
to force the connection to close before reading any more data but after the request is finished.
Relatively simple - looks something like this:
class GracefulRequestHandler(tornado.web.RequestHandler):
def initialize(self):
if self.application.is_shutting_down:
self.set_header('Connection', 'close')
self.request.connection.no_keep_alive = True
Respondido el 03 de Septiembre de 14 a las 22:09
1
finish()
doesn't apply here because a connection in the "keep-alive" state is not associated with a RequestHandler
. In general there's nothing you can (or need to) do with a keep-alive connection except close it, since the browser isn't listening for a response.
Websockets are another story - in that case you may want to close the connections yourself before shutting down (but don't have to - your clients should be robust against the connection just going away).
Respondido 12 Feb 14, 18:02
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas python sockets tornado or haz tu propia pregunta.
I get that about
finish()
, what do I need to do on the Application object to close connections? I understand that it's not a huge issue to just suddenly shut down, but I'm wondering if there's a way to instruct tornado to close all open connections. - jeff tratnerThere's not currently any way to close all open connections except by shutting down the process. - ben darnell
Okay, thanks - that's the answer I was looking for :) - jeff tratner
with Tornado 4 keeping track of all connections, you could just iterate over
app._connections
and close each one, right? - jeff tratnerHTTPServer._connections
is not a public attribute, but there is now a close_all_connections function you can use (note that it's a coroutine so it returns a Future you must wait on) - ben darnell