According to pymongo documentation,
PyMongo is thread-safe and even provides built-in connection pooling for threaded applications.
I usually initiate my mongodb connection as follows:
import pymongo
db = pymongo.Connection()['mydb']
and then I can just use it like db.users.find ({'name': ..}) ...
Does this mean that I can actually place these two lines in lib / apps_global.py, for example:
class Globals(object):
def __init__(self, config):
self.cache = CacheManager(**parse_cache_config_options(config))
import pymongo
self.db_conn = pymongo.connection()
self.db = self.db_conn['simplesite']
and then in my base controller:
class BaseController(WSGIController):
def __call__(self, environ, start_response):
"""Invoke the Controller"""
ret = WSGIController.__call__(self, environ, start_response)
app_globals.db_conn.end_request()
return ret
And start calling the app_global db variable on all controllers? Hope it's really that simple.
source
share