Adding new handler to running python tornado server

删除回忆录丶 提交于 2019-12-18 05:13:08

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/31802664/adding-new-handler-to-running-python-tornado-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!