问题描述
我是 Python Tornado 服务器的新手,我正在为我的下一个必须在实时环境中工作的项目评估 Python Tornado.我已经使用 Web Socket 实现从 github 运行了一个示例代码.
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.
这是示例代码片段.
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 应用程序动态添加新路由和处理程序,而无需重新启动侦听端口的服务器.
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.
例如;服务器开始运行并为路由/"提供 index.html,它有 n 个查看器.如果一个新的需求伴随着路由/foo"来提供 foo.html 而不会阻塞路由/"的 n 个查看者.如果有的话,有哪些不重启服务器的可能处理方式.
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.
推荐答案
你需要 tornado.web.Application
的 add_handlers
方法;像这样使用它:
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.
这篇关于添加新的处理程序来运行 python Tornado 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!