django + PyMongo pooling的最佳做法? [英] best practice of django + PyMongo pooling?

查看:157
本文介绍了django + PyMongo pooling的最佳做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 db = pymongo.Connection()在Django的views.py中调用一个简单的MongoDB连接来存储一些简单的统计信息。



使自动支持MongoDB连接池的最佳做法是什么?



我需要在哪里放置 end_request ()代码?



如何在连接期间选择 max_pool_size 参数? / p>

解决方案

连接池如何在PyMongo中工作?


每个连接实例都有内置的连接池。默认情况下,
每个线程在其第一个操作上保留自己的套接字。那些
套接字被保留,直到该线程调用end_request()。



调用end_request()可以将套接字返回到池中
并由其他线程使用,而不是创建一个新的套接字。
这个方法的正确使用对于许多
线程或长时间运行的线程,对PyMongo
操作进行调用很重要。



或者,使用auto_start_request = False创建的连接将
在所有线程中共享套接字(安全)。


我认为它归结于您拥有的应用程序类型以及请求将持有多长时间的连接。调用 end_request 的想法有助于长时间运行的请求,持续很长一段时间,并导致许多套接字被创建。如果单个请求可以在不再需要连接的情况下释放连接,则可以将套接字重新用于其他请求。



如果他们是快速请求,那么我认为 auto_start_request = False 通过重用套接字来工作。 >

确保连接使用相同的套接字意味着将具有一致的读取。想想如果你做了一个查询,但它被延迟了,然后immeditely另一个查询,它使用了一个不同的套接字。此套接字设置在之前的响应。您将有不一致的数据,因为它不反映以前的写入。


I have a db = pymongo.Connection() call in Django's views.py for a simple MongoDB connection to store some simple statistics.

What's the best practice to make it auto support MongoDB connection pooling?

Where do I need to put the end_request() code?

How do I choose the max_pool_size parameter during connection?

解决方案

How does connection pooling work in PyMongo?

Every Connection instance has built-in connection pooling. By default, each thread gets its own socket reserved on its first operation. Those sockets are held until end_request() is called by that thread.

Calling end_request() allows the socket to be returned to the pool, and to be used by other threads instead of creating a new socket. Judicious use of this method is important for applications with many threads or with long running threads that make few calls to PyMongo operations.

Alternatively, a Connection created with auto_start_request=False will share sockets (safely) among all threads.

I think it comes down to the type of application you have and how long the requests will hold onto a connection. The idea of calling end_request helps with long running requests holding on to a socket for a long time and causing many sockets to get created. If a single request can release the connection when it no longer needs it, then the socket can be repurposed for other requests.

If they are fast requests, then I believe the auto_start_request=False works by reusing the socket.

Ensuring a connection keeps using the same socket means that is will have consistent reads. Think if you made a query but it got delayed, and then immeditely made another query and it used a different socket. This socket manages to respond before the previous. You would have inconsistent data since it does not reflect the previous write.

这篇关于django + PyMongo pooling的最佳做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆