returning a value from a thread

Jeff Shannon jeff at
Wed Jul 14 23:02:52 CEST 2004

Christopher T King wrote:

>How about:
>blocking_IO_thread = threading.Thread(target=blocking_IO_func)
>workerthread = threading.Thread(target=somefunc)
>Neither of those needs multiple processors to show (possibly huge)  
>performance gains.  

Hmmm... I suppose so, although it seems to me that in most cases, you 
will be waiting for multiple chunks of blocking IO (in which case the 
worker thread should feed them through a queue for the main thread to 
process as they arrive), or needing to do an arbitrary number of screen 
updates / other user requests before the thread is finished (i.e., the 
"responsive UI" case I mentioned).  The blocking IO case does have some 
merit, I'll admit -- I may be somewhat biased by the problems I've been 
working on, where blocking IO hasn't been an issue, so I didn't consider 
this.  (I still have to say that I can't imagine *many* cases where one 
would have background processing where a specific number of UI 
interactions is appropriate -- if you need UI updates, then you're 
probably going to need an arbitrary number of them, in which case you 
need to check whether the thread is done rather than wait for it -- but 
I suppose that such cases may exist.)

>Either way, I didn't make this problem up: the OP
>asked how to get a value back from a thread (a reasonable thing to do),
>not whether doing such a thing was the correct way to code his problem
>(which I have no reason to doubt).

You did, however, give an answer that will only work effectively in a 
particular subset of cases that fit the OP's description, without being 
specific about those limitations.  It may well be that your solution 
will work for the OP, but since this list is archived and frequently 
searched, it's always good to explain *why* a given solution works or 
doesn't work.  The OP is not the only person who'll be reading, so it is 
(at least IMO) beneficial to speak to a somewhat more general case, or 
at least to be clear about what cases one *is* speaking to.

Jeff Shannon
Credit International

More information about the Python-list mailing list