-1

I have the following, seemingly easy, issue which I cannot figure out.

I made a short, self-containing example below:

from flask import Flask


class MyFlask(Flask):

    def __init__(self, name):
        super().__init__(name)
        print(f'Initialising Flask app {id(self)}')

    def __del__(self):
        print(f'Deleting Flask app {id(self)}')


if __name__ == "__main__":
    app = MyFlask(__name__)
    app.run(host='0.0.0.0', port=5000, debug=True)

When one runs this, you will see two instances of MyFlask being initialized, but only one being destroyed. I know why there are two calls to the init method (the way Werkzeug works), but why is only one destroyed?

See the sample output below:

(venv) D:\Onedrive\Documents\Projects\FlaskReloader>python example.py
Initialising Flask app 1944027544880
 * Serving Flask app "example" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Restarting with stat
Initialising Flask app 2213899877680
 * Debugger is active!
 * Debugger PIN: 247-475-647
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
Deleting Flask app 1944027544880

Any ideas? Thanks!

George
  • 93
  • 5

1 Answers1

-1

I think it cause by the override of Flask.__del__

Call super on that __del__ or don't override

print(f'Deleting Flask app {id(self)}')
super().__del__(self)
Reznik
  • 2,663
  • 1
  • 11
  • 31
  • The override is there simply to demonstrate the issue by printing the informative message. If the override were the issue, it would not print anything to begin with. But the MyFlask.__del__ method is being called once. – George Jun 15 '20 at 17:24
  • @George i know, but the `Flask.__del__` wasnt called – Reznik Jun 15 '20 at 17:25