【问题标题】:Adding new handler to running python tornado server将新处理程序添加到运行 python tornado 服务器
【发布时间】:2015-10-26 11:13:14
【问题描述】:

我是 python tornado 服务器的新手,我正在为我的下一个必须在实时环境中工作的项目评估 python tornado。我已经从 github 运行了一个带有 Web Socket 实现的示例代码。

这是示例代码 sn-p。

    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()

代码按预期工作并且正常。

是否可以提供类似云的解决方案,以便我可以向 Web 应用程序动态添加新路由和处理程序,而无需重新启动侦听端口的服务器。

例如;服务器开始运行并为路由“/”提供 index.html,它有 n 个查看器。如果一个新的需求伴随着路由 '/foo' 被提供 foo.html 而不会阻塞路由 '/' 的 n 个查看者。如果有的话,在不重新启动服务器的情况下有哪些可能的处理方式。

【问题讨论】:

    标签: python tornado


    【解决方案1】:

    您需要tornado.web.Applicationadd_handlers 方法;像这样使用它:

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

    从它的代码来看,它并没有阻塞任何东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多