returning a value from a thread

Christopher T King squirrel at WPI.EDU
Wed Jul 14 16:27:49 EDT 2004


On Wed, 14 Jul 2004, Jeff Shannon wrote:

> If you *are* going to join(), then the advantages of concurrent 
> execution get thrown away while sitting at that join().  I see no 
> practical advantage of this:
> 
>     workerthread = threading.Thread(target=somefunc)
>     workerthread.run()
>     somestuff()
>     someotherstuff()
>     workerthread.join()
> 
> vs this:
> 
>     somestuff()
>     someotherstuff()
>     somefunc()
> 
> The only way you could possibly get a speed advantage from using a 
> thread is on a multiprocessor machine, and I don't believe that Python 
> currently makes use of multiple processors anyhow (though I think that 
> it's possible for C extension modules to do so).  The flow-control 
> advantage of using a thread, that your program flow doesn't have to wait 
> for the task to finish, is lost when you are waiting to join().  All 
> you're left with is the added complexity.

How about:

blocking_IO_thread = threading.Thread(target=blocking_IO_func)
blocking_IO_thread.run()
somestuff()
someotherstuff()
blocking_IO_thread.join()

or:

workerthread = threading.Thread(target=somefunc)
workerthread.run()
do_screen_updates()
wait_for_user_request()
workerthread.join()

Neither of those needs multiple processors to show (possibly huge)  
performance gains.  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).





More information about the Python-list mailing list