Idle wait for outstanding threads?

Alan Kennedy alanmk at hotmail.com
Thu Jan 31 06:34:06 EST 2002


Michael, 

Thanks for the suggestion.

Michael Kelly wrote in message 
> There is a module called Futures that simplifies manipulating
> subtasks using threads.  It might not be what you need in this
> case but should be a good candidate for your library even so.
> It uses the metaphor of waiting for a function result.  You
> launch your thread then call a method that returns the result.
> You can block or not block but the basic idea is that if the
> main thread has no other tasks you block on the function
> result to wait for thread completion.

Indeed, this is the paradigm that I am trying to implement: function
calls evaluated in separate threads. BTW, the URL is

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/84317

> If appropriate for your program I guess you would launch
> all your tasks and then finish up main thread business,
> then block on all the thread method results etc..

However, "Future" is not what I need, as far as I can tell. The recipe
provided for Future relates only to a single thread (I think). I
launch a function in a separate thread (Future) and then do a blocking
wait, which does a non/blocking 'wait' on a threading.Condition var
until the function call thread has 'notify'ed on the Condition.

But if I want to wait on more than one "Future", it is not possible,
because I cannot do a blocking 'wait' on more than one Condition var
at a time. Instead I have to do a non-blocking wait on each of the
"Future"s separately: I'm back to the busy wait, i.e. a tight while
loop that consumes a lot of CPU.

Really what I need is a threading equivalent of the "select"
call for file descriptors. "select" allows me to block on multiple
file descriptors simultaneously, and return when something arrives on
any one of them. I need something equivalent for threads, that allows
me to block on multiple 'thread.lock's or multiple
'threading.Condition's at the same time. Also, something that had a
timeout as well, just like the "select" call, would be ideal.

Regards,

Alan.



More information about the Python-list mailing list