473,385 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 2639
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Olivier Parisy | last post by:
Hi, I like to use thread to simplify the handling of independant, blocking tasks. But controling them from a main thread is not always easy to do in a clean way. So I've written some generic...
0
by: Ajay | last post by:
hi! i have a main application which is run through a GUI. the GUI has a function 'start server' which starts a server in another thread and a stop function which should stop this server. the...
1
by: benmorganpowell | last post by:
I have a small windows service which connects to a POP3 server at defined intervals, scans the available messages, extracts the required information and inserts the data into a SQL database. I am...
3
by: Terry Olsen | last post by:
I'm trying to shutdown my service by stopping all the started classes & threads. In each Class I have a Public Sub like this: Public Sub StopIntf() Try Threading.Thread.CurrentThread.Abort()...
5
by: taylorjonl | last post by:
I am completely baffled. I am writting a daemon application for my work to save me some time. The application works fine at my home but won't work right here at work. Basically I have a...
1
by: deluded.soul | last post by:
Hi everyone, I am trying to use the boost multithreading library. I am having a problem as the join() function for the thread never returns. I am using a boolean variable to indicate when the...
2
by: tshad | last post by:
I have a Service that starts a thread that never ends. When I stop the service from the Service Applet, does it kill the thread or do I need to do it myself? If it isn't killed, what happens...
2
by: Steve | last post by:
Hi All, I've been trying to come up with a good way to run a certain process at a timed interval (say every 5 mins) using the SLEEP command and a semaphore flag. The basic thread loop was always...
1
by: Chris M. Thomasson | last post by:
FWIW, here is a quick example (in the form of a fully compliable program with error checking omitted) of how I use POSIX threads within a C++ environment:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.