[Baypiggies] Threading question

Shannon -jj Behrens jjinux at gmail.com
Thu Apr 27 00:37:16 CEST 2006


On 4/26/06, Danny Yoo <dyoo at hkn.eecs.berkeley.edu> wrote:
>
>
> On Wed, 26 Apr 2006, Ari Krupnik wrote:
>
> > I find myself writing these two lines all the time:
> >
> > t=myThread()
> > t.start()
> >
> > Is there any reason to not call start() from the constructor?
>
> It depends.  In the example above, things should work find if we go
> directly and say:
>
>      myThread().start()
>
> But do we want to be able to say t.wait() later on?  If so, we'll need to
> keep a name to the constructed thread, or else we won't have a way of
> sending additional messages to that thread.

If it makes you happy, write a function like:

def construct_and_run(*args, **kargs):
    thread = construct_it(*args, **kargs)
    thread.start()
    return thread

my_thread = construct_and_run(...)

However, be forewarned that the thread might do quite a bit of stuff
before construct_and_run returns and before my_thread gets set.  That
is, it's a race condition.

Happy Hacking!
-jj


More information about the Baypiggies mailing list