Paul Rubin <http://[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] writes: > > The main problem with this is that the yield leaves the lock locked. > > If any other thread wants to read the generator it will block. > > Ouch, good catch. Do you see a good fix other than try/finally? > Is there a classical way to do it with coroutines and semaphores? Jesse's solution from the other half of this thread, generalized: import Queue class ReentrantIterator(Queue.Queue): def _init(self, iterator): self.iterator = iterator def _empty(self): return False def _get(self): return self.iterator.next() def _put(*ignore): raise TypeError, "Can't put to a ReentrantIterator" def next(self): return self.get() def __iter__(self): return self Now, x=ReentrantIterator(itertools.count()) should have all the properties we want, I think. The locking is thanks of Queue.Queue and its sweet implementation of the Template Method design pattern. Alex -- http://mail.python.org/mailman/listinfo/python-list