Skip to content

Instantly share code, notes, and snippets.

@robspychala
Created July 18, 2011 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robspychala/1088452 to your computer and use it in GitHub Desktop.
Save robspychala/1088452 to your computer and use it in GitHub Desktop.
eventlet is slower than sync pymongo queries?
# > time python async_pymongo.py
#
# real 0m0.511s
# user 0m0.220s
# sys 0m0.170s
import pymongo
import eventlet
eventlet.monkey_patch()
POOL_SIZE = 10
class SocketWrapper(object):
__slots__ = ('__pool','__sock')
def __init__(self,pool,sock):
self.__pool = pool
self.__sock = sock
def __getattr__(self,name):
return getattr(self.__sock,name)
def __del__(self):
self.__pool.sockets.put(self.__sock)
del self.__pool
del self.__sock
class Pool(object):
def __init__(self,socket_factory,max_pool_size=POOL_SIZE):
self.socket_factory = socket_factory
self.max_pool_size = min(max_pool_size,POOL_SIZE)
self.sockets = None
def socket(self):
if not self.sockets:
self.initialize()
return SocketWrapper(self,self.sockets.get())
def initialize(self):
self.sockets = Queue(self.max_pool_size)
for i in range(self.max_pool_size):
sock = self.socket_factory()
self.sockets.put(sock)
def return_socket(self):
pass
pymongo.connection._Pool = Pool
db = pymongo.Connection().test
def do(*args, **kwargs):
db.test2.insert(*args)
pool = eventlet.GreenPool(size=1000)
pile = eventlet.GreenPile(pool)
[pool.spawn_n(do, {'num': i}) for i in xrange(1000)]
pool.waitall()
# > time python sync_pymongo.py
#
# real 0m0.356s
# user 0m0.120s
# sys 0m0.150s
import pymongo
c = pymongo.Connection()
db = c.test
for i in xrange(1000):
db.test2.insert({'num': i})
@colinhowe
Copy link

Did you ever get anywhere with this?

My suspicion is that the overhead of eventlet is actually more than the cost of an individual insert when doing it against a local server without any other load hitting it and no indices to worry about.

@robspychala
Copy link
Author

yep exactly. if the mongo server was on a big 32 core box I bet the results would be different. then it (mongo) would be able to do the work in parallel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment