473,383 Members | 1,798 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,383 software developers and data experts.

(Synchronous) Thread Control

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
its tasks.

At the end of this message, you'll find an example of the kind
of code I'm using. Here is its output :

Thread: starting
Main: waiting 5 seconds
Thread: beating
Thread: beating
Thread: beating
Thread: beating
Main: done sleeping, stopping thread
Wrapper: joining thread
Thread: beating
Thread: exiting
It seems like the main thread never exits from join(), in spite of
the timeout and the likeliness of the sub thread ending.

Any idea of what I've done wrong?

Best regards,
Olivier.

import threading, time

class IntThread:

def start(self):
launched = threading.Event() # Initially false
self.listener = self.Listener(launched)
self.listener.start()
launched.wait()

def stop(self):
self.listener.stop()
print "Wrapper: joining thread"
self.listener.join(5) # Should be more than enought
print "Wrapper: after join()"

class Listener(threading.Thread):

def __init__(self, event):
threading.Thread.__init__(self)
self.do_stop = False
self.event = event

def stop(self):
self.do_stop = True

def run(self):
print "Thread: starting"
self.event.set()
while not self.do_stop:
time.sleep(1)
print "Thread: beating"
print "Thread: exiting"

def test():
thd = IntThread()
thd.start()
delay = 5
print "Main: waiting %s seconds" % delay
time.sleep(delay)
print "Main: done sleeping, stopping thread"
thd.stop()
print "Main: end of program"

test()
Jul 18 '05 #1
2 2389
Olivier Parisy wrote:
It seems like the main thread never exits from join(), in spite of
the timeout and the likeliness of the sub thread ending.

Any idea of what I've done wrong?


Nothing at all. It works fine here. (And nice clean code
you have, by the way.)

Are you running this from the command line, or inside some
other tool, or something else unusual? What versions of
Python and OS are you using?

I'm on Win XP Pro SP 2 with Python 2.3.4.

-Peter
Jul 18 '05 #2
phansen wrote:
Olivier Parisy wrote:
It seems like the main thread never exits from join(), in spite of
the timeout and the likeliness of the sub thread ending.

Any idea of what I've done wrong?
Nothing at all. It works fine here. (And nice clean code
you have, by the way.)


Hey, thanks! High-level languages are nice in that you can spend more
time on polish, indeed :-)

Are you running this from the command line, or inside some
other tool, or something else unusual? What versions of
Python and OS are you using?


Gosh, you are right: this definitely works fine in the command line.
I was running this from a Linux file manager (using "launch in a
terminal" and "keep the terminal open" options), but something
seems to be wrong with this approach.

Regards,
Olivier.
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Noel | last post by:
Hi, I am a tad confused about if there are any benefits from using asynchronous vs synchronous network communication. As my example, I have been writing a dns lookup stack with a network...
4
by: Ryan Liu | last post by:
TcpClient has a method called GetworkStream GetStream(); So in other words, there is only one stream associate with it for input and output, right? So while it is receiving, it can not send, and...
0
ammoos
by: ammoos | last post by:
hi friends pls help me.. i got an assignment which i feel very difficult to me.. i dont have more knowledge about multi-threading in .net... the assignment details is below.... pls help me... i...
2
by: ng01 | last post by:
In AjaxPro, if you leave out the Callback parameter, it becomes a synchronous call. Is it possible to perform a synchronous call with ASP.NET AJAX? Please limit responses to the question...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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
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.