Most viewed recipes tagged "gevent"http://code.activestate.com/recipes/tags/gevent/views/2011-09-02T05:56:58-07:00ActiveState Code RecipesMongoDB Pool for gevent and pymongo packages (Python)
2011-09-02T05:56:58-07:00Andrey Nikishaevhttp://code.activestate.com/recipes/users/4176176/http://code.activestate.com/recipes/577490-mongodb-pool-for-gevent-and-pymongo-packages/
<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>
Observer Design Pattern for python gevent coroutine package (Python)
2010-12-08T08:33:30-08:00Andrey Nikishaevhttp://code.activestate.com/recipes/users/4176176/http://code.activestate.com/recipes/577491-observer-design-pattern-for-python-gevent-coroutin/
<p style="color: grey">
Python
recipe 577491
by <a href="/recipes/users/4176176/">Andrey Nikishaev</a>
(<a href="/recipes/tags/event/">event</a>, <a href="/recipes/tags/gevent/">gevent</a>, <a href="/recipes/tags/observer/">observer</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>This is simple implementation of the observer design pattern. Acting as a registration hub, it fires events when requested.
Also i have gevent.Timeout like interface in situations when you need to run event-method in the same greenlet. Example: </p>
<pre class="prettyprint"><code>e = Observer()
ev = e.wait('kill')
try:
gevent.sleep(3)
except FiredEvent:
print 'Fired!'
else:
print 'Not Fired!'
finally:
ev.cancel()
</code></pre>
<p>But rememeber, if you are using subscribe method, event-method will be executed in another greenlet.</p>