如何让瓶子在文件更改时重新启动?

2024-05-29 00:26:52 发布

您现在位置:Python中文网/ 问答频道 /正文

到目前为止,我真的很喜欢Bottle,但是每次我进行代码更改时都必须从服务器中按CTRL+C并重新启动它,这对我的工作效率是一个很大的影响。我考虑过使用Watchdog来跟踪文件的更改,然后重新启动服务器,但是当bottle.run函数阻塞时,我如何才能做到这一点。

从监视文件更改的外部脚本运行服务器似乎需要设置很多工作。我认为这是一个普遍的问题,瓶子,樱桃和等开发商。

谢谢你解决这个问题!


Tags: 文件函数run代码服务器脚本瓶子bottle
3条回答

从教程中查看一个名为"Auto Reloading"的部分

During development, you have to restart the server a lot to test your recent changes. The auto reloader can do this for you. Every time you edit a module file, the reloader restarts the server process and loads the newest version of your code.

这给出了以下示例:

from bottle import run
run(reloader=True)

run(reloader=True)

有些情况下,它不会重新加载,就像导入在def中一样。强迫我重新加载

subprocess.call(['touch', 'mainpgm.py'])

在linux中重新加载很好。

run()中使用reloader=True。请记住,在windows中,由于multiprocessing模块的工作方式,它必须位于if __name__ == "__main__":之下。

from bottle import run

if __name__ == "__main__":
    run(reloader=True)

相关问题 更多 >

    热门问题