¿Cerrar todas las conexiones de enchufe (mantener vivo) en tornado?
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.
preguntado el 12 de febrero de 14 a las 06:02
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.
initialize()I check for this and if we are shutting down, I set the
Connection: closeheader to comply with the RFC and then also set
self.request.connection.no_keep_alive = Trueto 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
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).
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 tratner
There'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._connectionsand close each one, right? - jeff tratner
HTTPServer._connectionsis 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