根据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变量?我希望真的有那么简单。
发布于 2011-04-05 17:00:03
Pylons的作者Ben Bangert用mongodb写了他的博客引擎。你是can browse it's source code online。
https://stackoverflow.com/questions/4528681
复制相似问题