Suggested generator to add to threading module.

Alan Kennedy alanmk at hotmail.com
Mon Jan 19 07:02:33 EST 2004


[Andrae Muys]
> 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.  

Not exactly.

Queue.Queue is a *thread-safe* communication primitive: you're not
required to have seperate threads at both ends of a Queue.Queue, but
it is guaranteed to work correctly if you do have multiple threads.

>From the module documentation

"""
The Queue module implements a multi-producer, multi-consumer FIFO
queue. It is especially useful in threads programming when information
must be exchanged safely between multiple threads. The Queue class in
this module implements all the required locking semantics. It depends
on the availability of thread support in Python. 
"""

http://www.python.org/doc/current/lib/module-Queue.html

> serialise() (at least the
> corrected version discussed later in this thread) is strictly a
> synchronisation primitive.

Just as Queue.Queue is a synchronisation primitive: a very flexible
and useful primitive that happens to be usable in a host of different
scenarios.

I think I'm with Aahz on this one: when faced with this kind of
problem, I think it is best to use a tried and tested inter-thread
communication paradigm, such as Queue.Queue. In this case, Queue.Queue
fits the problem (which is just a variation of the producer/consumer
problem) naturally. Also, I doubt very much if there is much excessive
resource overhead when using Queue.Queues.

As you've already seen from your first cut of the code, writing
thread-safe code is an error-prone process, and it's sometimes
difficult to figure out all the possibile calling combinations when
multiple threads are involved.

But if you'd used Queue.Queue, well this whole conversation would
never have come up, would it ;-)

regards,

-- 
alan kennedy
------------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan:              http://xhaus.com/contact/alan



More information about the Python-list mailing list