Top-rated recipes tagged "pymongo" Code RecipesMongoDB Pool for gevent and pymongo packages (Python) 2011-09-02T05:56:58-07:00Andrey Nikishaev <p style="color: grey"> Python recipe 577490 by <a href="/recipes/users/4176176/">Andrey Nikishaev</a> (<a href="/recipes/tags/gevent/">gevent</a>, <a href="/recipes/tags/mongodb/">mongodb</a>, <a href="/recipes/tags/pymongo/">pymongo</a>, <a href="/recipes/tags/python/">python</a>). Revision 2. </p> <p>Wrote some simple implementation of pool for pymongo package under gevent coroutine library.</p> <p>Base bug here was with pymongo.connection.Pool because in the original package it is thread-local, so when you spawn new greenlet and trying to get already open connection, it creates new connection because in this greenlet pool is empty. So if you will implement your own pool don’t forget about this.</p> <h4 id="example-of-use">Example of use:</h4> <pre class="prettyprint"><code># Create Pool. db = Mongo('test_db',10) # Get connection from pool conn = db.get_conn() # Get raw connection for GridFS raw_conn = conn.getDB #Mongo is a singleton. So if you want to get connection in another part of application just type db = Mongo() conn = db.get_conn() #Connection will get back to pool when context will be closed. </code></pre>