Commits

Ryan Williams  committed b837a33

Improved implentation of iterators in tpool; thanks to Slant

  • Participants
  • Parent commits fea522f

Comments (0)

Files changed (3)

 * 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
+* Benoit Chesneau, bug report on green.os and patch to fix it
+* Slant, better iterator implementation in tpool
+* Ambroff, nice pygtk hub

File eventlet/tpool.py

     # 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):
         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)
 
 
 

File tests/tpool_test.py

         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):