bottle坑爹的reload

这两天用bottle在写一个小应用,里面我开了一个线程,专门监听远程一台服务器。那台服务器对重复连接作了处理,会断开之前的旧连接,结果当我运行下面的代码时,总是提示重连:

import threading
t = threading.Timer(2, long_connect)
t.start()

from bottle import run
run(app,host='localhost',port=8000,reloader=True)

其中long_connect是我用于监听那台服务器的函数,那里一直会提示我:远程连接已断开,开始重连。

这事折腾了我好几天,今天偶然看了一下任务管理器,发现执行上面的代码会启动两个python进程,这是啥原因,google了半天没发现有用的信息,难道别人都没遇到过。

自己翻文档吧,然后看到这么一段:

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)

How it works: the main process will not start a server, but spawn a new child process using the same command line arguments used to start the main process. All module-level code is executed at least twice! Be careful.

不知道各位看到我标红的那段是什么感受,反正我是快崩溃了。

你妈,打开reloader所有的代码会执行两次啊!!

哎,以后一定要小心这些参数,真心坑爹啊。

PS:flask听说跟bottle类似,所以又去看了看它的文档,用的是user_reloader,不过倒是没提具体是怎么实现的,估计还得去挖源码,就不深究了。

原文地址:https://www.cnblogs.com/chaoswong/p/3064527.html