generators shared among threads
Alex Martelli
aleaxit at yahoo.com
Tue Mar 7 22:54:33 EST 2006
Paul Rubin <http://phr.cx@NOSPAM.invalid> wrote:
> jess.austin at gmail.com 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
More information about the Python-list
mailing list