Connection pooling in flask application

815 views
Skip to first unread message

Snehal Parmar

unread,
Mar 25, 2015, 3:12:38 AM3/25/15
to mongod...@googlegroups.com
Want to know the performance of using flask-pymongo extension for making connections to mongodb from flask applications ?
I want to know how is the connection pooling using this extension works. Please provide some information about the topic. 

A. Jesse Jiryu Davis

unread,
Mar 25, 2015, 5:29:11 PM3/25/15
to mongod...@googlegroups.com
Looks to me like Flask-PyMongo does the right thing, it creates one MongoClient for your application and keeps it cached for use by all requests in your application process:


Therefore, Flask-PyMongo's connection pooling works the same as when you use PyMongo directly with a single MongoClient instance:


In other words, it uses connection pooling correctly and you don't need to do anything special if you use Flask-PyMongo as designed.

Snehal Parmar

unread,
Mar 26, 2015, 1:32:30 AM3/26/15
to mongod...@googlegroups.com
Thanks, I'm using like the following:

    app.config['MONGO_URI'] = 'mongodb://ip:port/db_name'
    mongo = PyMongo(app, config_prefix='MONGO')

after then in my api function in flask i use:
    db = mongo.db

I want to know how many connections will there and used here ?

A. Jesse Jiryu Davis

unread,
Mar 26, 2015, 5:56:21 AM3/26/15
to mongod...@googlegroups.com
Hi Snehal. As the PyMongo docs say, "The client opens sockets on demand to support the number of concurrent MongoDB operations your application requires. The size of each connection pool is capped at maxPoolSize, which defaults to 100." Your application will open, at the most, as many sockets to MongoDB as there are concurrent threads processing requests in Flask. But in reality you'll open far fewer MongoDB connections than that if all your MongoDB operations are quick.
Reply all
Reply to author
Forward
0 new messages