Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep a shared functional object in memory in Django?

I have an object that wraps some Active Directory functions which are used quite frequently in my codebase. I have a convenience function to create it, but each time it is creating an SSL connection which is slow and inefficient. The way I can improve this in some places is to pass it to functions in a loop but this is not always convenient.

The class is state-free so it's thread-safe and could be shared within each Django instance. It should maintain its AD connection for at least a few minutes, and ideally not longer than an hour. There are also other non-AD objects I would like to do the same with.

I have used the various cache types, including in-memory, is it appropriate to use these for functional objects? I thought they were only meant for (serializable) data.

Alternatively: is there a Django suitable pattern for service locators or connection pooling like you often seen in Java apps?

Thanks, Joel

like image 547
jbyrnes Avatar asked Sep 07 '25 12:09

jbyrnes


1 Answers

I have found a solution that appears to work well, which is simply a Python feature that is similar to a static variable in Java.

def get_ad_service():
    if "ad_service" not in get_ad_service.__dict__:
        logger.debug("Creating AD service")
        get_ad_service.ad_service = CompanyADService(settings.LDAP_SERVER_URL,
                            settings.LDAP_USER_DN,
                            settings.LDAP_PASSWORD)

        logger.debug("Created AD service")
    else:
        logger.debug("Returning existing AD service")

    return get_ad_service.ad_service

My code already calls this function to get an instance of the AD service so I don't have to do anything further to make it persistent.

I found this and similar solutions here: What is the Python equivalent of static variables inside a function?

Happy to hear alternatives :)

like image 134
jbyrnes Avatar answered Sep 10 '25 05:09

jbyrnes