Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding new handler to running python tornado server

Tags:

python

tornado

I'm new to python tornado server, and I were evaluating python tornado for my next project that has to work on real time environment. I've run a sample code from github with Web Socket implementation.

this is the sample code snippet.

    app = web.Application([
       (r'/', IndexHandler),
       (r'/ws', SocketHandler),
       (r'/api', ApiHandler),
       (r'/(favicon.ico)', web.StaticFileHandler, {'path': '../'}),
       (r'/(rest_api_example.png)', web.StaticFileHandler, {'path': './'}),
   ])

   if __name__ == '__main__':
       app.listen(8080)
       ioloop.IOLoop.instance().start()

The code works as expected and fine.

Whether it is possible to give a cloud like solution so that I could add new routes and handlers dynamically to the web application without restarting the server listening a port.

For example; The server starts running and serves index.html for the route '/' and it has n viewers. If a new requirement came with route '/foo' to be served foo.html without blocking the n viewers of route '/'. What are the possible ways to handle without restarting the server, if any.

like image 280
Sony George Avatar asked Aug 04 '15 06:08

Sony George


1 Answers

You'll need the tornado.web.Application's add_handlers method; use it like this:

app.add_handlers(
    r".*",  # match any host
    [
        (
            r"/foo/([^/]*)",
            FooHandler
        ),
        (
            r"/bar/([^/]*)",
            BarHandler
        ),
    ]
)

Judging by its code, it does not block anything.

like image 135
Andrew Kravchuk Avatar answered Sep 22 '22 15:09

Andrew Kravchuk