Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #12708

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

From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: Why doesn't threading.join() return a value?
Date 2011-09-03 12:51 -0400
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <roy-A4ECCE.12510303092011@news.panix.com> (permalink)
References <61044fa5-2850-4f05-a55a-d61521c74313@d7g2000vbv.googlegroups.com> <5da6bf87-9412-46c4-ad32-f8337d56b2c3@o15g2000vbe.googlegroups.com> <87hb4u3isf.fsf@dpt-info.u-strasbg.fr> <bf50c8e1-1476-41e1-b2bc-61e329bfa5be@s12g2000yqm.googlegroups.com> <j3rdg00fmp@news6.newsguy.com>

Show all headers | View raw


In article <j3rdg00fmp@news6.newsguy.com>,
 Chris Torek <nospam@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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Why doesn't threading.join() return a value? Roy Smith <roy@panix.com> - 2011-09-02 07:53 -0700
  Re: Why doesn't threading.join() return a value? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-03 01:45 +1000
    Re: Why doesn't threading.join() return a value? Seebs <usenet-nospam@seebs.net> - 2011-09-02 16:42 +0000
      Re: Why doesn't threading.join() return a value? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-03 06:49 +1000
  Re: Why doesn't threading.join() return a value? Adam Skutt <askutt@gmail.com> - 2011-09-02 11:01 -0700
    Re: Why doesn't threading.join() return a value? Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2011-09-02 20:23 +0200
      Re: Why doesn't threading.join() return a value? Adam Skutt <askutt@gmail.com> - 2011-09-02 11:53 -0700
        Re: Why doesn't threading.join() return a value? Chris Torek <nospam@torek.net> - 2011-09-02 20:14 +0000
          Re: Why doesn't threading.join() return a value? Adam Skutt <askutt@gmail.com> - 2011-09-02 15:02 -0700
          Re: Why doesn't threading.join() return a value? Roy Smith <roy@panix.com> - 2011-09-03 12:51 -0400
        Re: Why doesn't threading.join() return a value? Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2011-09-03 10:33 +0200
          Re: Why doesn't threading.join() return a value? Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2011-09-03 11:00 +0200
        Re: Why doesn't threading.join() return a value? Carl Banks <pavlovevidence@gmail.com> - 2011-09-03 05:27 -0700
    Re: Why doesn't threading.join() return a value? Roy Smith <roy@panix.com> - 2011-09-02 19:16 -0400
      Re: Why doesn't threading.join() return a value? Chris Torek <nospam@torek.net> - 2011-09-03 01:04 +0000
    Re: Why doesn't threading.join() return a value? Carl Banks <pavlovevidence@gmail.com> - 2011-09-03 05:04 -0700

csiph-web