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() 2 2414
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 amount of dns lookups I have one dns provider with
many state objects passing through (and a high...
|
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. What does "Synchronous" mean?
Means while it is reading or waiting for data to arrive from the...
|
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 & Asynchronous Threading.
Requirements
1) A form with
a) a button to initialize threads
...
|
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.
|
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...
| |
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...
|
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 most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |