473,545 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,m ybignum):

Thread.__init__ (self)

self.mybignum=m ybignum

def run(self):

for l in range(10):
for k in range(self.mybi gnum):
res=0
for i in range(self.mybi gnum):
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(big num):
#We create 2 Thread objects for the 2 threads.
thr1=MyThread(b ignum)
thr2=MyThread(b ignum)

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(big num)
stoptime=time.c lock()

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

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

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

test()

Sep 2 '06 #1
1 2651
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(second s)
t.setDaemon(Tru e)
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__=="__ma in__":
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
2396
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 code whose purpose is to start and stop threads in a synchronous (blocking) way from the caller's point of view. Hence, after start() is called you...
0
1545
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 code is from Tkinter import * import tkMessageBox import tkFileDialog
1
4460
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 assuming that this is not an uncommon piece of software. I want to get an architecture that conforms as closely as possible with the...
3
1623
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() Catch End Try End Sub
5
2202
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 MainForm what has a Start/Stop button that starts and stops the processing thread. private void StartButton_Click(object sender, System.EventArgs e) {...
1
7722
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 thread should stop running. Here is basically a stub of what I am doing: Code:
2
1938
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 when I start the service again. ************************************************** Protected Overrides Sub OnStart(ByVal args() As String) Dim...
2
3343
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 sitting in the sleep command and not able to be interrupted. When the time came to set the semaphore flag to false (stopping the thread), my...
1
3567
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: _________________________________________________________________ /* Simple Thread Object ______________________________________________________________*/ #include <pthread.h> extern...
0
7486
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7416
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7676
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7442
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7776
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6001
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3456
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1032
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
729
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.