让我们以 Tornado 主页中的 hello world 应用程序为例:
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
application = tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
Run Code Online (Sandbox Code Playgroud)
有没有办法,在 IOloop 启动后而不停止它,基本上停止应用程序并启动另一个应用程序(在同一端口或另一个端口上)?
我看到我可以在运行时添加新应用程序(侦听不同端口),但我不知道如何停止现有应用程序。
Application.listen()方法实际上创建了一个HTTPServer并调用了它的listen()方法。HTTPServer对象具有stop()可能是您需要的方法。但是为了做到这一点,您必须HTTPServer在脚本中明确创建对象。
server = HTTPServer(application)
server.listen(8888)
tornado.ioloop.IOLoop.instance().start()
#somewhere in your code
server.stop()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2099 次 |
| 最近记录: |