pkavumba / django-vectordb

A fast and scalable app that adds vector search capabilities to your Django applications. It offers low latency, fast search results, native Django integration, and automatic syncing between your models and the vector index. Incremental updates are also supported out of the box.
Apache License 2.0
68 stars 6 forks source link

model loaded twice #24

Closed TheWiseC0der closed 6 months ago

TheWiseC0der commented 7 months ago

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]
pkavumba commented 7 months ago

Hi! Thanks for raising the issue. Will work on fixing it tomorrow or the other day

pkavumba commented 6 months ago

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