From 85ffa7659849db167abf59a4f8eaa6af60f511ec Mon Sep 17 00:00:00 2001 From: "nat.linden" Date: Sat, 14 Jun 2008 04:25:53 -0700 Subject: [PATCH] [svn r127] Tidy up the implementation of CoroutinePool.wait_all() by overriding Pool.get() and Pool.put() to manage our metaphore, rather than inserting calls inline. --- eventlet/coros.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/eventlet/coros.py b/eventlet/coros.py index 6e1ec63..28810a3 100644 --- a/eventlet/coros.py +++ b/eventlet/coros.py @@ -437,12 +437,7 @@ class CoroutinePool(pools.Pool): """ Private method that runs the function, catches exceptions, and passes back the return value in the event.""" try: - try: - result = func(*args, **kw) - finally: - # Be sure to decrement requested coroutines, no matter HOW we - # leave. (See _execute().) - self.requested.dec() + result = func(*args, **kw) if evt is not None: evt.send(result) except api.GreenletExit, e: @@ -461,11 +456,6 @@ class CoroutinePool(pools.Pool): def _execute(self, evt, func, args, kw): """ Private implementation of the execute methods. """ - # Track number of requested coroutines. Note the asymmetry: we - # increment when the coroutine is first REQUESTED, long before it - # actually starts up; we don't decrement until it completes. (See - # _safe_apply().) - self.requested.inc() # if reentering an empty pool, don't try to wait on a coroutine freeing # itself -- instead, just execute in the current coroutine if self.free() == 0 and api.getcurrent() in self._greenlets: @@ -484,6 +474,20 @@ class CoroutinePool(pools.Pool): sender = event() self._greenlets.add(api.spawn(self._main_loop, sender)) return sender + + def get(self): + """Override of eventlet.pools.Pool interface""" + # Track the number of requested CoroutinePool coroutines + self.requested.inc() + # forward call to base class + return super(CoroutinePool, self).get() + + def put(self, item): + """Override of eventlet.pools.Pool interface""" + # forward call to base class + super(CoroutinePool, self).put(item) + # Track the number of outstanding CoroutinePool coroutines + self.requested.dec() def execute(self, func, *args, **kw): """Execute func in one of the coroutines maintained