Suggested generator to add to threading module.

Aahz aahz at
Mon Jan 19 06:37:33 CET 2004

In article <7934d084.0401181808.6e698042 at>,
Andrae Muys <amuys at> wrote:
>aahz at (Aahz) wrote in message news:<bu998t$o62$1 at>...
>> In article <7934d084.0401152058.164a240c at>,
>> Andrae Muys <amuys at> wrote:
>>>Found myself needing serialised access to a shared generator from
>>>multiple threads.  Came up with the following
>>>def serialise(gen):
>>>  lock = threading.Lock()
>>>  while 1:
>>>    lock.acquire()
>>>    try:
>>>      next =
>>>    finally:
>>>      lock.release()
>>>    yield next
>> I'm not sure this is generic enough to go in the standard library.
>> Usually, I'd recommend that someone wanting this functionality consider
>> other options in addition to this (such as using Queue.Queue()).
>I'm curious to know how a Queue.Queue() provides the same
>functionality?  I have always considered a Queue.Queue() to be an
>inter-thread communcation primitive. serialise() (at least the
>corrected version discussed later in this thread) is strictly a
>synchronisation primitive.

Well, yes; Queue.Queue() provides both synchronization *and* data
protection.  In some ways, it's overkill for this specific problem, but
my experience is that there are so many different ways to approach this
class of problems and so many ways to screw up threaded applications,
it's best to learn one swiss-army knife that can handle almost everything
you want to throw at it.
Aahz (aahz at           <*>

A: No.
Q: Is top-posting okay?

More information about the Python-list mailing list