Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pymongo connection pooling and client requests

Tags:

python

pymongo

I know pymongo is thread safe and has an inbuilt connection pool.

In a web app that I am working on, I am creating a new connection instance on every request.

My understanding is that since pymongo manages the connection pool, it isn't wrong approach to create a new connection on each request, as at the end of the request the connection instance will be reclaimed and will be available on subsequent requests.

Am I correct here, or should I just create a single instance to use across multiple requests?

like image 756
treecoder Avatar asked Jun 10 '12 12:06

treecoder


People also ask

How does connection pooling work in PyMongo?

How does connection pooling work in PyMongo? ¶ Every MongoClient instance has a built-in connection pool per server in your MongoDB topology. These pools open sockets on demand to support the number of concurrent MongoDB operations that your multi-threaded application requires.

Is PyMongo asynchronous?

Is PyMongo asyncronous? According to their documentation (https://pymongo.readthedocs.io/en/stable/faq.html), PyMongo fully supports Gevent: So yes, if implemented correctly, PyMongo is able to use an asynchronous framework like Gevent.

Do I need to close PyMongo connection?

There's no need to close a Connection instance, it will clean up after itself when Python garbage collects it. You should use MongoClient instead of Connection ; Connection is deprecated. To take advantage of connection pooling, you could create one MongoClient that lasts for the entire life of your process.

When should I use connection pooling?

Connection pooling is great for scalability - if you have 100 threads/clients/end-users, each of which need to talk to the database, you don't want them all to have a dedicated connection open to the database (connections are expensive resources), but rather to share connections (via pooling).


1 Answers

The "wrong approach" depends upon the architecture of your application. With pymongo being thread-safe and automatic connection pooling, the actual use of a single shared connection, or multiple connections, is going to "work". But the results will depend on what you expect the behavior to be. The documentation comments on both cases.

If your application is threaded, from the docs, each thread accessing a connection will get its own socket. So whether you create a single shared connection, or request a new one, it comes down to whether your requests are threaded or not.

When using gevent, you can have a socket per greenlet. This means you don't have to have a true thread per request. The requests can be async, and still get their own socket.

In a nutshell:

  • If your webapp requests are threaded, then it doesn't matter which way you access a new connection. The result will be the same (socket per thread)
  • If your webapp is async via gevent, then it doesn't matter which way you access a new conection. The result will be the same. (socket per greenlet)
  • If your webapp is async, but NOT via gevent, then you have to take into consideration the notes on the best suggested workflow.
like image 97
jdi Avatar answered Oct 14 '22 17:10

jdi