I am trying to develop an API using the Flask micro web platform but I have hit an issue when I noticed that the same object is being created multiple times as multiple log entries are getting posted and also unnecessary memory usage is occurring. I am running Python 3.7 on 64 bit Windows 10 desktop computer.
I have condensed this down to the simplest working example that still exhibits the issue:
from flask import Flask
class de(object):
def __init__(self):
print(f'__init__ : {self}')
def __del__(self):
print(f'__del__ : {self}')
def create_app(test_config=None):
app = Flask(__name__)
det = de()
return app
The output running flask run -p 2094 is:
* Serving Flask app "APP-Account-Api/src" (lazy loading)
* Environment: development
* Debug mode: on
* Restarting with stat
* Debugger is active!
* Debugger PIN: 201-506-894
* Running on http://127.0.0.1:2094/ (Press CTRL+C to quit)
__init__ : <src.de object at 0x000001AE906C4A90>
__del__ : <src.de object at 0x000001AE906C4A90>
__init__ : <src.de object at 0x0000023488C73C50>
__del__ : <src.de object at 0x0000023488C73C50>
As you can see I only create the test class once but the log is showing two sets of creation and destruction.