Why doesn't threading.join() return a value?

Roy Smith roy at panix.com
Sat Sep 3 12:51:03 EDT 2011


In article <j3rdg00fmp at news6.newsguy.com>,
 Chris Torek <nospam at torek.net> wrote:

> For that matter, you can use the following to get what the OP asked
> for.  (Change all the instance variables to __-prefixed versions
> if you want them to be Mostly Private.)
> 
> import threading
> 
> class ValThread(threading.Thread):
>     "like threading.Thread, but the target function's return val is captured"
>     def __init__(self, group=None, target=None, name=None,
>             args=(), kwargs=None, verbose=None):
>         super(ValThread, self).__init__(group, None, name, None, None, 
>         verbose)
>         self.value = None
>         self.target = target
>         self.args = args
>         self.kwargs = {} if kwargs is None else kwargs
> 
>     def run(self):
>         "run the thread"
>         if self.target:
>             self.value = self.target(*self.args, **self.kwargs)
> 
>     def join(self, timeout = None):
>         "join, then return value set by target function"
>         super(ValThread, self).join(timeout)
>         return self.value

Yeah, that's pretty much what I had in mind.  I'm inclined to write up a 
PEP proposing that this become the standard behavior of 
threading.Thread.  It seems useful, and I can't see any way it would 
break any existing code.



More information about the Python-list mailing list