当启用SANIC的调试模式时,SANIC将提供更详细的日志记录输出,默认情况下将启用自动重新加载功能。
设置调试模式:通过设置 debug 模式将输出Sanic更详细的输出,并激活自动重新加载程序。提供debug级别的日志。
from sanic import Sanic
from sanic.response import json
app = Sanic(__name__)
@app.route('/')
async def hello_world(request):
return json({"hello": "world"})
if __name__ == '__main__':
app.run(host="127.0.0.1", port=8000, debug=True)
图片
手动设置自动重新加载:SANIC提供了一种手动启用或禁用自动重新加载的方法,即 auto_reload 参数将激活或停用自动重新加载。
from sanic import Sanic
from sanic.response import json
app = Sanic(__name__)
@app.route('/')
async def hello_world(request):
return json({"hello": "china"})
if __name__ == '__main__':
app.run(host="127.0.0.1", port=8000, debug=True, auto_reload=True)
在把world 改成 china后 按下键盘 Ctrl + S 保存。程序会主动重新启动,而不在需要点击运行来更新程序执行当前代码。
最终效果:
图片