Why does running the Flask dev server run itself twice?

前端 未结 6 2122
南旧
南旧 2020-11-22 01:39

I\'m using Flask for developing a website and while in development I run flask using the following file:

#!/usr/bin/env python
from datetime import datetime
         


        
6条回答
  •  鱼传尺愫
    2020-11-22 02:31

    If you are using the modern flask run command, none of the options to app.run are used. To disable the reloader completely, pass --no-reload:

    FLASK_DEBUG=1 flask run --no-reload
    

    Also, __name__ == '__main__' will never be true because the app isn't executed directly. Use the same ideas from Martijn's answer, except without the __main__ block.

    if os.environ.get('WERKZEUG_RUN_MAIN') != 'true':
        # do something only once, before the reloader
    
    if os.environ.get('WERKZEUG_RUN_MAIN') == 'true':
        # do something each reload
    

提交回复
热议问题