pymongo connection pooling and client requests

前端 未结 1 1312
梦谈多话
梦谈多话 2021-02-02 14:39

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 reques

1条回答
  •  说谎
    说谎 (楼主)
    2021-02-02 15:11

    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.

    0 讨论(0)
提交回复
热议问题