Mondomb Pylons

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"""
        # WSGIController.__call__ dispatches to the Controller method
        # the request is routed to. This routing information is
        # available in environ['pylons.routes_dict']
        ret = WSGIController.__call__(self, environ, start_response)

        # Don't forget to release the thread for mongodb
        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.

+3
source share
1 answer

Ben Bangert, author of Pylons, wrote his blog engine with mongodb. You can view the source code online .

+1
source

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


All Articles