Python瓶子运行两次初始化方法[英] Python bottle runs initialization method twice

本文是小编为大家收集整理的关于Python瓶子运行两次初始化方法的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我对瓶子有问题,_initialize功能是两次运行的.示例应用程序:

 @route("/index")
 def index():
      return "bang"

 def _initialize():
      print("bam")

 if __name__ == "__main__":
     _initialize()
     run(reloader=True, host="localhost", port = 8990)

输出为:

bam
bam
Bottle v0.11.rc1 server starting up (using WSGIRefServer())...                             
Listening on http://localhost:8080/                                                        
Hit Ctrl-C to quit.

为什么会发生这种情况,我该如何在瓶中进行这样的初始化?

推荐答案

问题是reloader=True run函数的参数.参见重新加载句子:

所有模块级代码至少执行两次!小心.

本文地址:https://www.itbaoku.cn/post/1975354.html

问题描述

I've got a problem with bottle, the _initialize function is run twice. Example app:

 @route("/index")
 def index():
      return "bang"

 def _initialize():
      print("bam")

 if __name__ == "__main__":
     _initialize()
     run(reloader=True, host="localhost", port = 8990)

The output is:

bam
bam
Bottle v0.11.rc1 server starting up (using WSGIRefServer())...                             
Listening on http://localhost:8080/                                                        
Hit Ctrl-C to quit.

Why is it happening and how can I do such pre init in bottle?

推荐答案

The problem is the reloader=True argument for the run function. See http://bottlepy.org/docs/dev/tutorial.html#auto-reloading for the sentence:

All module-level code is executed at least twice! Be careful.