From 59d625cee12c283c7aebec691a38b4de2eb6d934 Mon Sep 17 00:00:00 2001 From: Ryan Williams Date: Wed, 17 Mar 2010 17:42:28 -0700 Subject: [PATCH] Improved implentation of iterators in tpool; thanks to Slant --- AUTHORS | 4 +++- eventlet/tpool.py | 9 +++++++-- tests/tpool_test.py | 24 ++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/AUTHORS b/AUTHORS index fda8cb6..74aceb4 100644 --- a/AUTHORS +++ b/AUTHORS @@ -44,4 +44,6 @@ Thanks To * Cesar Alaniz, for uncovering bugs of great import * the grugq, for contributing patches, suggestions, and use cases * Ralf Schmitt, for wsgi/webob incompatibility bug report and suggested fix -* Benoit Chesneau, bug report on green.os and patch to fix it \ No newline at end of file +* Benoit Chesneau, bug report on green.os and patch to fix it +* Slant, better iterator implementation in tpool +* Ambroff, nice pygtk hub \ No newline at end of file diff --git a/eventlet/tpool.py b/eventlet/tpool.py index b9b2e11..580ad0b 100644 --- a/eventlet/tpool.py +++ b/eventlet/tpool.py @@ -184,8 +184,6 @@ class Proxy(object): # the following are a buncha methods that the python interpeter # doesn't use getattr to retrieve and therefore have to be defined # explicitly - def __iter__(self): - return proxy_call(self._autowrap, self._obj.__iter__) def __getitem__(self, key): return proxy_call(self._autowrap, self._obj.__getitem__, key) def __setitem__(self, key, value): @@ -209,6 +207,13 @@ class Proxy(object): return len(self._obj) def __nonzero__(self): return bool(self._obj) + def __iter__(self): + if iter(self._obj) == self._obj: + return self + else: + return Proxy(iter(self._obj)) + def next(self): + return proxy_call(self._autowrap, self._obj.next) diff --git a/tests/tpool_test.py b/tests/tpool_test.py index bb954a7..36eafde 100644 --- a/tests/tpool_test.py +++ b/tests/tpool_test.py @@ -138,6 +138,30 @@ class TestTpool(LimitedTestCase): for i in prox: result.append(i) self.assertEquals(range(10), result) + + @skip_with_pyevent + def test_wrap_iterator2(self): + def foo(): + import time + for x in xrange(10): + yield x + time.sleep(0.01) + + counter = [0] + def tick(): + for i in xrange(100): + counter[0]+=1 + eventlet.sleep(0.001) + + gt = eventlet.spawn(tick) + previtem = 0 + for item in tpool.Proxy(foo()): + self.assert_(item >= previtem) + # make sure the tick happened at least a few times so that we know + # that our iterations in foo() were actually tpooled + self.assert_(counter[0] > 10) + gt.wait() + @skip_with_pyevent def test_raising_exceptions(self):