Suggested generator to add to threading module.

Andrae Muys nospam at no.spam
Sat Jan 17 10:08:56 CET 2004


Aahz wrote:
> In article <7934d084.0401152058.164a240c at posting.google.com>,
> Andrae Muys <amuys at shortech.com.au> 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 = gen.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()).

While I fully appreciate the importance of a Queue.Queue in implementing 
a producer/consumer task relationship, this particular function provides 
serialised access to a *passive* data-stream.  With the increasing 
sophistication of itertools and I feel there maybe an argument for 
supporting shared access to a generator.

Anyway I thought it was worth offering as a possible bridge between the 
itertools and threading modules.  If I'm mistaken, then it's no major loss.

Andrae




More information about the Python-list mailing list