Spawing a thread and printing dots until it finishes

sophie_newbie paulgeeleher at gmail.com
Tue Apr 22 10:10:07 EDT 2008


Hi, I'm trying to write a piece of code that spawns a thread and
prints dots every half second until the thread spawned is finished.
Code is
something like this:

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?

Any ideas?

Thanks!



More information about the Python-list mailing list