pymongo connection pooling and client requests

2019-03-11 02:22发布

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?

1条回答
做个烂人
2楼-- · 2019-03-11 03:22

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.
查看更多
登录 后发表回答