Spawing a thread and printing dots until it finishes

D'Arcy J.M. Cain darcy at druid.net
Tue Apr 22 17:41:23 CEST 2008


On Tue, 22 Apr 2008 07:10:07 -0700 (PDT)
sophie_newbie <paulgeeleher at gmail.com> wrote:
> import threading
> class MyThread ( threading.Thread ):
>         def run ( self ):
>                 myLongCommand()...
> 
> import time
> 
> t = MyThread()
> t.start()
> 
> while t.isAlive():
>         print "."
>         time.sleep(.5)
> 
> print "OK"
> 
> The thing is this doesn't print a dot every half second. It just
> pauses for ages until the thread is finished and prints prints ".OK".
> But if I take out the "time.sleep(.5)" line it will keep printing dots
> really fast until the thread is finished. So it looks like its the
> time.sleep(.5) bit that is messing this up somehow?

We know that your main routine gives up the processor but without a
full definition of MyThread how do we know that it ever does?  I
suspect that it hits sleep once, if at all, and then goes to the final
print statement.  In fact, I suspect that this is not exactly what you
tried anyway.  This code would not have printed ".OK" whether it
entered the loop or not.  It could have printed this;

.
OK

because the print statement in the loop will print a dot on a line by
itself.

When looking for these sorts of answers you should really try to create
a smallest program that exhibits the behaviour you are questioning and
then cut and paste the entire script into your message unedited.  Often
enough you will even answer your own question in the process.

-- 
D'Arcy J.M. Cain <darcy at druid.net>         |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.



More information about the Python-list mailing list