Python bottle runs initialization method twice

为君一笑 提交于 2019-12-10 03:38:07

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/13064490/python-bottle-runs-initialization-method-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!