In article <j3rdg00...@news6.newsguy.com>,
 Chris Torek <nos...@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.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to