472,142 Members | 1,032 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,142 software developers and data experts.

thread join() not stopping

Here is a simple piece of thread code.
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.
I have a function that I must have timeout and cannot figure a way to do
this.

Any help appreciated. Should compile on 2.4

Bryan

import time
from threading import Thread

class MyThread(Thread):

def __init__(self,mybignum):

Thread.__init__(self)

self.mybignum=mybignum

def run(self):

for l in range(10):
for k in range(self.mybignum):
res=0
for i in range(self.mybignum):
res+=1
#########################################
print "not stopping"
##########################################

def myadd_nothread(bignum):

for l in range(10):
for k in range(bignum):
res=0
for i in range(bignum):
res+=1
for l in range(10):
for k in range(bignum):
res=0
for i in range(bignum):
res+=1

def thread_test(bignum):
#We create 2 Thread objects for the 2 threads.
thr1=MyThread(bignum)
thr2=MyThread(bignum)

thr1.start()
thr2.start()

thr1.join(2)
thr2.join(2)
def test():

bignum=150
#Let us test the threading part

starttime=time.clock()
thread_test(bignum)
stoptime=time.clock()

print "Running 2 threads took %.3f seconds" % (stoptime-starttime)

#Now run without Threads.
starttime=time.clock()
myadd_nothread(bignum)
stoptime=time.clock()

print "Running Without Threads took %.3f seconds" % (stoptime-starttime)
if __name__=="__main__":

test()

Sep 2 '06 #1
1 2483
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

Sep 2 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Olivier Parisy | last post: by
reply views Thread by Ajay | last post: by
3 posts views Thread by Terry Olsen | last post: by
5 posts views Thread by taylorjonl | last post: by
1 post views Thread by deluded.soul | last post: by
2 posts views Thread by Steve | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.