model loaded twice
Closed this issue · 2 comments
currently get two instances. when loading my app it loads the model twice at startup. can you guys not use a metaclass for it instead? never get problems with that way of singleton pattern.
"""metaclass for the Singleton pattern
metaclasses are used for class creation, they are like classes for classes, their logic specifies how the class should behave, not the instances of the class"""
class SingletonMeta(type):
"""
The Singleton class can be implemented in different ways in Python. Some
possible methods include: base class, decorator, metaclass. We will use the
metaclass because it is best suited for this purpose.
"""
# We'll store instances of the Singleton classes here.
_instances = {}
# The call method is called when the class is called, it is like the init method for the class
def call(cls, *args, **kwargs):
"""
Possible changes to the value of the __init__
argument do not affect
the returned instance.
"""
if cls not in cls._instances:
instance = super().call(*args, **kwargs)
cls._instances[cls] = instance
return cls._instances[cls]
Hi! Thanks for raising the issue. Will work on fixing it tomorrow or the other day
Hi! I have spent some time working to address this issue by implementing a Singleton pattern via Metaclasses. Despite this, the problem persisted. After conducting numerous tests, I realized that the recurring model loading issue in the development environment occurs because Django reloads the app soon after its creation. Therefore, even the solution you suggested was not ineffective. Running the server with the --noreload
option confirmed that the app is indeed reloaded, which results in the model being loaded a twice despite using a singleton pattern