473,835 Members | 1,836 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 2678
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
2421
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 are garanteed that the thread is running, and after stop() you know it has completed
0
1563
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
4476
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 recommendations from Microsoft on developing Windows Services, but to be honest I have found difficultly in...
3
1633
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
2226
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) { if( bStopSignal ) { // disable controls that aren't valid when running
1
7763
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
1952
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 theThread As Thread
2
3377
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 program would have to wait up to the entire sleep time to break out of the loop. I have finally found...
1
3598
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 "C" void* thread_entry(void*);
0
9810
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9653
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10815
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10524
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10236
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9348
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7768
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5639
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3092
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.