与Mongodb的铁塔

时间:2010-12-24 23:22:07

标签: mongodb pylons

根据pymongo文档,

PyMongo is thread-safe and even provides built-in connection pooling for threaded applications.

我通常会像这样启动我的mongodb连接:

import pymongo
db = pymongo.Connection()['mydb']

然后我可以像db.users.find({'name':..})一样使用它...

这是否意味着我实际上可以将这两行放在lib / apps_global.py中:

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']

然后在我的基本控制器中:

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

并开始在我的控制器中调用app_global的db变量? 我希望它真的那么容易。

1 个答案:

答案 0 :(得分:1)

Pylons的作者Ben Bangert用mongodb写了他的博客引擎。 你can browse it's source code online