简体   繁体   中英

Connection Pooling in Mongo with PyMongo

I would like to makeup a setup to store my mongodb connections as 3-4 threads on Memory by which it will act as a pool of connections. I don't want to create a connection everytime when my core functions work which does some db queries. I am thinking on this way like If I have a pool of connections then my core functions will take available connections(available threads) from the pool, use it and release it back to the pool.

Does it make any sense? Is it possible to achieve this?

I know that mongodb internally do have connection pooling, but I would like to the above mentioned stuff on top of it.

Your question doesn't really make sense. There's no need to do anything like have connection pooling on top of existing connection pooling.

PyMongo docs : All that really matters is whether your application uses multithreading or multiprocessing.

  • multithreading is already handled by the connection pools and you only need one client instance.
  • multiprocessing requires you to create a new instance for each process to avoid any deadlock issues.

Your app is likely multithreaded, so all you should need to do is have a global instance of your db connection and reuse it for each db query. PyMongo will take care of the rest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM