Flask正在使用Werkzug的底层run_with_reloader函数(在serving.py中找到)。。。它本身使用在同一文件中先前创建的restart_with_reloader和{}函数。在
run_with_reloader生成另一个python进程(使用传递给第一个进程的所有相同参数再次运行Werkzug),这个新进程使用thread模块生成一个运行服务器函数的新线程或子进程。然后运行reloader_loop并等待。在
reloader_loop只需循环访问所有已导入的模块,并获取其最后修改日期。然后在指定的时间间隔(默认为1秒)再次检查所有文件,看它们是否已被修改。如果有,则当前运行的(slave)Werkzug进程退出(终止),退出代码为3。一旦它退出,它所启动的线程或子进程(实际上正在执行该工作)也将终止。主进程检查退出代码是否为3。如果是的话,它会生成一个新的从子进程,就像以前一样。否则,它将使用相同的退出代码退出。在
以下代码供参考:def reloader_loop(extra_files=None, interval=1):
"""When this function is run from the main thread, it will force other
threads to exit when any modules currently loaded change.
Copyright notice. This function is based on the autoreload.py from
the CherryPy trac which originated from WSGIKit which is now dead.
:param extra_files: a list of additional files it shou