让我们以 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()
有没有办法在 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()
关于python - 停止 Tornado 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23778213/