Python和mongoDB连接池(pymongo)

时间:2012-11-22 03:14:08

标签: python mongodb pymongo

我有一个Web应用程序,每分钟都有数千个请求。 以下是我的mongoDB连接的python代码:

Tool.py:

globalconnection = None

def getCollection(name,safe=False,readpref=ReadPreference.PRIMARY):

    global globalconnection
    while globalconnection is None:
            try:
                    if not globalconnection is None:
                            globalconnection.close()
                    globalconnection = Connection('mongodb://host:port',replicaSet='mysetname',safe=False,read_preference=ReadPreference.PRIMARY,network_timeout=30,max_pool_size=1024)
            except Exception as e:
                    globalconnection = None

    request_context.connection = globalconnection

    return request_context.connection["mydb"]["mycoll"]

web.py

@app.route("/test")
def test():
    request_collection = getCollection("user")
    results = request_collection.find()
    for result in results:
        #do something...
        request_collection.save(result)
    request_collection.end_request()

一个http请求通过此函数获取连接,

并且http请求在请求结束之前调用end_request。

但我发现在增加请求时,mongoDB中存在许多AutoReconnect错误和超过20000个连接。

你有什么建议吗?

1 个答案:

答案 0 :(得分:2)

  1. 对于自动重新连接,您只需捕获异常,并尝试再次获取连接: http://api.mongodb.org/python/current/api/pymongo/errors.html

  2. 30秒的超时时间听起来太长了,请尝试缩短超时时间?

  3. 增加mongodb的最大连接数(默认值:20000) http://www.mongodb.org/display/DOCS/Connections