简体   繁体   English

使用 PyMongo 在 Mongo 中进行连接池

[英]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.我想构建一个设置来将我的 mongodb 连接存储为 Memory 上的 3-4 个线程,它将充当连接池。 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.我知道 mongodb 内部确实有连接池,但我想把上面提到的东西放在上面。

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. PyMongo 文档:真正重要的是您的应用程序是使用多线程还是多处理。

  • 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. PyMongo 将负责 rest。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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