I see there are many examples how to create singleton objects with python.
Is there a simple, elegant way to define singletons?
I would like to use a singleton in a FastAPI script and I am worried that the singleton item might not be cleared from memory once my script is done.
What I see in this example for instance
def singleton(cls):
instances = {}
def getinstance():
if cls not in instances:
instances[cls] = cls()
return instances[cls]
return getinstance
@singleton
class MyClass:
...
that the instance is managed within the decorator. How can I tell the decorator to free that memory piece when I finish all my logic? I had a similar issue before with Java long time ago and thinking here loud.
All I would like to do is instances = {}