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

查看:220
本文介绍了Python瓶运行两次初始化方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的瓶子有问题,_initialize函数运行了两次.示例应用程序:

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)

输出为:

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?

推荐答案

问题是run函数的reloader=True参数.请参见 http://bottlepy.org/docs/dev/tutorial.html#auto-重新加载:

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.

这篇关于Python瓶运行两次初始化方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆