Maintain a list of network connections in Tornado

I decided to keep track of multiple web socket connections. I need to save WebSocket Handler objects in a list. But I have several handlers - one for each WS URI (endpoint). Let's say my project has three end points - A, B, C

ws://www.example.com/A
ws://www.example.com/B
ws://www.example.com/C

So, to handle the connections for each of them, I have three handlers. Therefore, I am puzzled by where to create a list for storing handlers that will be used later.

My code before adding a list is

class WSHandlerA(tornado.websocket.WebSocketHandler):
    def open(self):
        print 'new connection'
        self.write_message("Hello World")

    def on_message(self, message):
        print 'message received %s' % message

    def on_close(self):
        print 'connection closed'


class WSHandlerB(tornado.websocket.WebSocketHandler):
    def open(self):
        print 'new connection'
        self.write_message("Hello World")

    def on_message(self, message):
        print 'message received %s' % message

    def on_close(self):
        print 'connection closed'

class WSHandlerB(tornado.websocket.WebSocketHandler):
    def open(self):
        print 'new connection'
        self.write_message("Hello World")

    def on_message(self, message):
        print 'message received %s' % message

    def on_close(self):
        print 'connection closed'

application = tornado.web.Application([
    (r'/A', WSHandlerA),
    (r'/B', WSHandlerB),
    (r'/C', WSHandlerC),
])


if __name__ == "__main__":
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

So, where can I create this list and make sure that it is visible to all created objects? I am also new to python and therefore have a little problem wrapping around myself.

, URI ( ) . WebSocket . , URI, .

.

+3
1

, , :

# Global variables.
a_handlers = []
b_handlers = []
c_handlers = []

WSHandlerA.open() a_handlers.append(self), WSHandlerB.open() - b_handlers.append(self) .. WSHandlerA.on_close() a_handlers.remove(self).

- A:

for handler in a_handlers:
    handler.write_message("message on A")

- :

for handler in a_handlers + b_handlers + c_handlers:
    # do something....
    pass

, Python set() , . add discard append remove.

+2

All Articles