473,573 Members | 2,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

(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(l aunched)
self.listener.s tart()
launched.wait()

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

class Listener(thread ing.Thread):

def __init__(self, event):
threading.Threa d.__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(dela y)
print "Main: done sleeping, stopping thread"
thd.stop()
print "Main: end of program"

test()
Jul 18 '05 #1
2 2398
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
4868
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 communication class switchable between asynchronous and synchronous. When I use asynchronous udp communication I find that if I want to process a large...
4
3296
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 vise visa, right? So will it be a problem both server and client can initiative a sending action? TcpClient only supports synchronous operation....
0
1858
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 have to submit this assignment today evening... please....... Windows Application to demonstrate the use of Multi-Threading with Synchronous &...
2
1751
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 posed...let's leave the discussions of why not async, or what does the "A" mean for another thread. Thanks for any help and examples of how to do it.
0
7699
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
8202
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...
1
7788
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
8066
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...
1
5594
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...
0
5292
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3733
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...
0
3733
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2216
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.