thread join() not stopping

mclaugb mclaugb at nospm.yahoo.com
Sat Sep 2 09:49:51 EDT 2006


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()








More information about the Python-list mailing list