threads or queue for this task

robin at execulink.com robin at execulink.com
Mon Sep 16 14:30:48 CEST 2002


"James J. Besemer" <jb at cascade-sys.com> wrote:

>On the other hand, you probably DO want to eliminate explicit sleeps 
>like this if you can.  E.g., the sleeps force a delay if data arrives to 
>be processed during the sleep.

That's what I was thinking.

>If you are clever, proper design of LookForWork() can automatically 
>suspend the threads when they have nothing to do.

I may sometimes be clever, and can no doubt cobble together code from
reading the docs (which I have been doing so far), but what I don't
know are some of the trade-offs of using certain constructs, or the
optimal way to do things. That is much more difficult to glean without
actual code examples.

For example, I really don't know what "proper design" I should use in
this case. Any sample code?

> E.g., that's one of 
>the great features of using a Queue -- threads are put to sleep if they 
>read an empty queue and they are woken up automatically when something 
>it put into the queue.  Similarly, if your thread is reading a Socket() 
>-- it'll sleep until data is available.  Then you don't need the 
>explicit sleeps.

The interface to Queue is deceptively simple. Are there constructs to
avoid or performance trade-offs to different methods? Again, any
explicit code (or at least explicit help as to which methods are
preferable) would be appreciated.

I am also put off by the description of the Queue Object, particularly
methods like empty() which says: "Return 1 if the queue is empty, 0
otherwise. Because of multithreading semantics, this is not reliable."

If it's not reliable, then what's the point of it existing? Why is it
not reliable? (I imagine simply because the thread doing the checking
could be superceded by the thread that implements the Queue in the
time it takes for the method to return and be acted upon.) Is it just
in certain cases that it's not reliable? How can one code if you don't
reliably know when a Queue is empty or full?

What this boils down to is: I'm sure I can do the job with the great
help I've already received, but do not have the confidence to know if
I'm doing the job the *right* way.

-- robin



More information about the Python-list mailing list