我正在使用与 Python 3 和 Linux 服务器一起使用的 Tornado,当我编辑和保存一些文本或 XML 文件时,我希望 Tornado 能够自行重启。我查了文档,发现这里有autoreload模块和watch功能。
它似乎只适用于 pyo 文件。如果我希望它在某个 URI 被修改时重新加载,我该怎么办?
我正在使用与 Python 3 和 Linux 服务器一起使用的 Tornado,当我编辑和保存一些文本或 XML 文件时,我希望 Tornado 能够自行重启。我查了文档,发现这里有autoreload模块和watch功能。
它似乎只适用于 pyo 文件。如果我希望它在某个 URI 被修改时重新加载,我该怎么办?
Setting the debug flag to True in settings forces Tornado to reload whenever a file is modified or whenever a URI is changed in app.py (or where ever you have defined your handlers). Tornado also automatically reloads template files so any changes in there will be seen instantly.
settings = {
'debug':True,
# other stuff
}
tornado.web.Application.__init__(self, handlers, **settings)
添加的文件必须是绝对路径。
def addwatchfiles(*paths):
for p in paths:
autoreload.watch(os.path.abspath(p))
addwatchfiles('config.xml')
config.xml 位于服务器的 python 文件开始的同一目录中。
你需要开启autoreload
:
tornado.autoreload.start()
tornado.autoreload.watch('myfile')