Threading question

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Threading question

iter
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?

Ari.

--
Elections only count as free and trials as fair if you can lose money
betting on the outcome.
_______________________________________________
Baypiggies mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: Threading question

Danny Yoo


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.

Best of wishes!
_______________________________________________
Baypiggies mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: Threading question

Shannon -jj Behrens
On 4/26/06, Danny Yoo <[hidden email]> 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
_______________________________________________
Baypiggies mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: Threading question

Kelly Yancey
In reply to this post by iter


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?
>
> Ari.
>
>

   If you want to t.setDaemon(), you need to do it before start().

   Kelly

_______________________________________________
Baypiggies mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/baypiggies