thread join() not stopping
Peter Otten
__peter__ at web.de
Sat Sep 2 10:38:38 EDT 2006
mclaugb wrote:
> When i add the print (or other) function into the run method--the thread
> fails to stop after 2 seconds which the join(2) should ensure.
The calling thread waits until the thread you call join() upon stops or 2
seconds have passed -- whatever occurs first.
import time
from threading import Thread
class MyThread(Thread):
tick = 0.1
def __init__(self, seconds):
Thread.__init__(self)
self.n = int(seconds/self.tick)
def run(self):
for i in range(self.n):
time.sleep(self.tick)
print "running"
def measure(seconds):
print "Run for %.2f seconds..." % seconds
t = MyThread(seconds)
t.setDaemon(True)
t.start()
starttime = time.time()
t.join(1) # wait one second
stoptime = time.time()
print stoptime-starttime
t.join() # wait for how long t takes to terminate
print "...done"
if __name__=="__main__":
measure(1.5)
measure(0.5)
Peter
More information about the Python-list
mailing list