MongoDB Link Caching in Django

I am using a standard (unlike NonRel) version of Django related to PostgreSQL on top of Apache + mod_wsgi. This setting also connects to MongoDB (some data is saved externally). Right now, I have to create a new MongoDB connection for each Django request and pass it along the entire call stack to all the functions that need access to MongoDB. Is there a way to cache connections between requests?

Edit

At the risk of blasphemy, will a global variable work in this case?

+6
source share
2 answers

There are several ways to explain how pymongo can work (or does not work) with mod_wsgi suggested here: http://api.mongodb.org/python/current/faq.html?highlight=wsgi#does-pymongo-work-with- mod-wsgi

Alternatively, you can use some sort of merge solution as described here: http://www.mongodb.org/display/DOCS/Notes+on+Pooling+for+Mongo+Drivers

One project that I know already has a union, MongoEngine , its a very simple ORM that uses pimongo backstage. You might want to learn it along with the pymongo faq solutions above.

+4
source

You can create a MongoDB connection somewhere and import it, as opposed to calling pymongo.connection.Connection() every time you need it. Or you can create a Singleton for this. Something like this in settings.py .

 class ConnectionSingleton(object): """Represents a MongoDB connection""" conn=None def __new__(cls,*args,**kwds): if cls.conn is None: cls.conn=pymongo.connection.Connection() return cls.conn 

Won't this solve your problem?

+2
source

Source: https://habr.com/ru/post/917857/


All Articles