473,840 Members | 1,594 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to force a thread to stop

Hi all,

Is there a way that the program that created and started a thread also stops
it.
(My usage is a time-out).

E.g.

thread = threading.Threa d(target=Loop.t estLoop)
thread.start() # This thread is expected to finish within a second
thread.join(2) # Or time.sleep(2) ?

if thread.isAlive( ):
# thread has probably encountered a problem and hangs
# What should be here to stop thread ??????

Note that I don't want to change the target (too much), as many possible
targets exist,
together thousands of lines of code.

Thanks,
Hans
Jul 22 '06
51 54905
br************* **********@yaho o.com writes:
Threadicide would not solve the problems you actually have, and it
tends to create other problems. What is the condition that makes
you want to kill the thread? Make the victim thread respond to that
condition itself.
If the condition is a timeout, one way to notice it is with sigalarm,
which raises an exception in the main thread. But then you need a way
to make something happen in the remote thread.
Jul 25 '06 #11

Dennis Lee Bieber wrote:
On Mon, 24 Jul 2006 10:27:08 -0700, "Carl J. Van Arsdall"
My problem with the fact that python doesn't have some type of "thread
killer" is that again, the only solution involves some type of polling
loop. I.e. "if your thread of execution can be written so that it

And that is because the control of a thread, once started, is
dependent upon the underlying OS...
No; it's because killing a thread from another thread fundamentally
sloppy.
The process of creating a thread can
be translated into something supplied by pretty much all operating
systems: an Amiga task, posix thread, etc.

But ending a thread is then also dependent upon the OS -- and not
all OSs have a way to do that that doesn't run the risk of leaking
memory, leaving things locked, etc. until the next reboot.
No operating system has a good way to do it, at least not for
the kind of threads Python offers.

The procedure for M$ Windows to end a task basically comes down to
"send the task a 'close window' event; if that doesn't work, escalate...
until in the end it throw its hands up and says -- go ahead and leave
memory in a mess, just stop running that thread".
The right procedure in MS Windows is the same as under POSIX:
let the thread terminate on its own.
module without killing the whole system. Any of you guys thought of a
way around this scenario?

Ask Bill Gates... The problem is part of the OS.
Or learn how to use threads properly. Linux is starting to get good
threading. Win32 has had it for quite a while.
--
--Bryan

Jul 25 '06 #12
br************* **********@yaho o.com wrote:
Carl J. Van Arsdall wrote:
[...]
>My problem with the fact that python doesn't have some type of "thread
killer" is that again, the only solution involves some type of polling
loop.

A polliing loop is neither required nor helpful here.

[...]
>#Just pretend for the sake of arguement that 'op' actually means
something and is a lengthy operation
def func_to_thread( ):
os.system('op 1')
os.system('op 2')
os.system('op 3')

What good do you think killing that thread would do? The
process running 'op n' has no particular binding to the thread
that called os.system(). If 'op n' hangs, it stays hung.

The problem here is that os.system doesn't give you enough
control. It doesn't have a timeout and doesn't give you a
process ID or handle to the spawned process.

Running os.system() in multiple threads strikes me as
kind of whacked. Won't they all compete to read and write
stdin/stdout simultaneously?
Unfortunately this is due to the nature of the problem I am tasked with
solving. I have a large computing farm, these os.system calls are often
things like ssh that do work on locations remote from the initial python
task. I suppose eventually I'll end up using a framework like twisted
but, as with many projects, I got thrown into this thing and threading
is where we ended up. So now there's the rush to make things work
before we can really look at a proper solution.
>
>#In order to make this killable with reasonable response time we have to
organize each of our ops into a function or something equally annoying

op_1():
os.system('op 1')

op_2():
os.system('op 2')

op_3():
os.system('op 3')

opList(op_1, op_2, op_3)
def to_thread():
for op in opList:
checkMessageQue ue()
op()

Nonsense. If op() hangs, you never get to checkMessageQue ue().
Yea, understood. At the same time, I can't use a timeout either, I
don't know how long op_1 or op_2 will be. This is why I want something
that is triggered on an event.

Now suppose op has a timeout. We could write

def opcheck(thing):
result = op(thing)
if result == there_was_a_tim eout:
raise some_timeout_ex ception

How is:

def func_to_thread( ):
opcheck('op 1')
opcheck('op 2')
opcheck('op 3')

any less managable than your version of func_to_thread?

Again, the problem I'm trying to solve doesn't work like this. I've
been working on a framework to be run across a large number of
distributed nodes (here's where you throw out the "duh, use a
distributed technology" in my face). The thing is, I'm only writing the
framework, the framework will work with modules, lots of them, which
will be written by other people. Its going to be impossible to get
people to write hundreds of modules that constantly check for status
messages. So, if I want my thread to "give itself up" I have to tell it
to give up. In order to tell it to give up I need some mechanism to
check messages that is not going to piss off a large team of
programmers. At the same time, do I really want to rely on other people
to make things work? Not really, I'd much rather let my framework
handle all control and not leave that up to programmers.

So the problem is, I have something linearly executed a large list of
python functions of various sizes ranging from short to long. Its not
about killing the thread so much as how do I make the thread listen to
control messages without polling.
>So with this whole "hey mr. nice thread, please die for me" concept gets
ugly quickly in complex situations and doesn't scale well at all.
Furthermore, say you have a complex systems where users can write
pluggable modules. IF a module gets stuck inside of some screwed up
loop and is unable to poll for messages there's no way to kill the
module without killing the whole system. Any of you guys thought of a
way around this scenario?

Threadicide would not solve the problems you actually have, and it
tends to create other problems. What is the condition that makes
you want to kill the thread? Make the victim thread respond to that
condition itself.

I feel like this is something we've established multiple times. Yes, we
want the thread to kill itself. Alright, now that we agree on that,
what is the best way to do that. Right now people keep saying we must
send the thread a message. That's fine and I completely understand
that, but right now the only mechanism I see is some type of polling
loop (or diving into the C API to force exceptions). So far I've not
seen any other method though. If you want to send a thread a control
message you must wait until that thread is able to check for a control
message. If something hangs in your thread you are totally screwed,
similarly, if your thread ends up in some excessively lengthy IO (IO
that could be interrupted or whatever) you have to wait for that IO to
finish before your thread can process any control messages.


--

Carl J. Van Arsdall
cv*********@mvi sta.com
Build and Release
MontaVista Software

Jul 25 '06 #13
On 2006-07-25 13:30:22, Carl J. Van Arsdall wrote:
>Running os.system() in multiple threads strikes me as kind of whacked.
Won't they all compete to read and write stdin/stdout simultaneously?
Unfortunately this is due to the nature of the problem I am tasked with
solving. I have a large computing farm, these os.system calls are often
things like ssh that do work on locations remote from the initial python
task.
[...]
Again, the problem I'm trying to solve doesn't work like this. I've been
working on a framework to be run across a large number of distributed
nodes (here's where you throw out the "duh, use a distributed
technology" in my face). The thing is, I'm only writing the framework,
the framework will work with modules, lots of them, which will be
written by other people. Its going to be impossible to get people to
write hundreds of modules that constantly check for status messages.
Doesn't this sound like a case for using processes instead of threads?
Where you don't have control over the thread, you can use a process and get
the separation you need to be able to kill this task.

Alternatively you could possibly provide a base class for the threads that
handles the things you need every thread to handle. They'd not have to
write it then; they'd not even have to know too much about it.

Gerhard

Jul 25 '06 #14
Gerhard Fiedler wrote:
On 2006-07-25 13:30:22, Carl J. Van Arsdall wrote:

>>Running os.system() in multiple threads strikes me as kind of whacked.
Won't they all compete to read and write stdin/stdout simultaneously?

Unfortunatel y this is due to the nature of the problem I am tasked with
solving. I have a large computing farm, these os.system calls are often
things like ssh that do work on locations remote from the initial python
task.

[...]

>Again, the problem I'm trying to solve doesn't work like this. I've been
working on a framework to be run across a large number of distributed
nodes (here's where you throw out the "duh, use a distributed
technology" in my face). The thing is, I'm only writing the framework,
the framework will work with modules, lots of them, which will be
written by other people. Its going to be impossible to get people to
write hundreds of modules that constantly check for status messages.

Doesn't this sound like a case for using processes instead of threads?
Where you don't have control over the thread, you can use a process and get
the separation you need to be able to kill this task.

Alternatively you could possibly provide a base class for the threads that
handles the things you need every thread to handle. They'd not have to
write it then; they'd not even have to know too much about it.

Gerhard

I'd be all for using processes but setting up communication between
processes would be difficult wouldn't it? I mean, threads have shared
memory so making sure all threads know the current system state is an
easy thing. With processes wouldn't I have to setup some type of
server/client design, where one process has the system state and then
the other processes constantly probe the host when they need the current
system state?


--

Carl J. Van Arsdall
cv*********@mvi sta.com
Build and Release
MontaVista Software

Jul 25 '06 #15
"Carl J. Van Arsdall" <cv*********@mv ista.comwrites:
I'd be all for using processes but setting up communication between
processes would be difficult wouldn't it? I mean, threads have
shared memory so making sure all threads know the current system
state is an easy thing. With processes wouldn't I have to setup
some type of server/client design, where one process has the system
state and then the other processes constantly probe the host when
they need the current system state? --
http://poshmodule.sf.net might be of interest.
Jul 25 '06 #16
On 2006-07-25 13:55:39, Carl J. Van Arsdall wrote:
I'd be all for using processes but setting up communication between
processes would be difficult wouldn't it? I mean, threads have shared
memory so making sure all threads know the current system state is an
easy thing.
I'm not sure about that. Sharing data between threads or processes is never
an easy thing, especially since you are saying you can't trust your module
coders to "play nice". If you can't trust them to terminate their threads
nicely when asked so, you also can't trust them to responsibly handle
shared memory. That's exactly the reason why I suggested processes.

With processes wouldn't I have to setup some type of server/client
design, where one process has the system state and then the other
processes constantly probe the host when they need the current system
state?
Anything else is bound to fail. You need to have safeguards around any
shared data. (A semaphore is a type of server/client thing...) At the very
least you need to prevent read access while it is updated; very rarely this
is an atomic action, so there are times where the system state is
inconsistent while it is being updated. (I don't think you can consider
many Python commands as atomic WRT threads, but I'm not sure about this.)
IMO, in the situation you are describing, it is an advantage that data is
not normally accessible -- this means that your module coders need to
access the data in the way you present it to them, and so you can control
that it is being accessed correctly.

Gerhard

Jul 25 '06 #17

Carl J. Van Arsdall wrote:
Unfortunately this is due to the nature of the problem I am tasked with
solving. I have a large computing farm, these os.system calls are often
things like ssh that do work on locations remote from the initial python
task. I suppose eventually I'll end up using a framework like twisted
but, as with many projects, I got thrown into this thing and threading
is where we ended up. So now there's the rush to make things work
before we can really look at a proper solution.
I don't get what threading and Twisted would to do for
you. The problem you actually have is that you sometimes
need terminate these other process running other programs.
Use spawn, fork/exec* or maybe one of the popens.

Again, the problem I'm trying to solve doesn't work like this. I've
been working on a framework to be run across a large number of
distributed nodes (here's where you throw out the "duh, use a
distributed technology" in my face). The thing is, I'm only writing the
framework, the framework will work with modules, lots of them, which
will be written by other people. Its going to be impossible to get
people to write hundreds of modules that constantly check for status
messages. So, if I want my thread to "give itself up" I have to tell it
to give up.
Threads have little to do with what you say you need.

[...]
I feel like this is something we've established multiple times. Yes, we
want the thread to kill itself. Alright, now that we agree on that,
what is the best way to do that.
Wrong. In your examples, you want to kill other processes. You
can't run external programs such as ssh as Python threads. Ending
a Python thread has essentially nothing to do with it.
Right now people keep saying we must send the thread a message.
Not me. I'm saying work the problem you actually have.
--
--Bryan

Jul 26 '06 #18
br************* **********@yaho o.com wrote:
Carl J. Van Arsdall wrote:

I don't get what threading and Twisted would to do for
you. The problem you actually have is that you sometimes
need terminate these other process running other programs.
Use spawn, fork/exec* or maybe one of the popens.
I have a strong need for shared memory space in a large distributed
environment. How does spawn, fork/exec allow me to meet that need?
I'll look into it, but I was under the impression having shared memory
in this situation would be pretty hairy. For example, I could fork of a
50 child processes, but then I would have to setup some kind of
communication mechanism between them where the server builds up a queue
of requests from child processes and then services them in a FIFO
fashion, does that sound about right?
Threads have little to do with what you say you need.

[...]
>I feel like this is something we've established multiple times. Yes, we
want the thread to kill itself. Alright, now that we agree on that,
what is the best way to do that.

Wrong. In your examples, you want to kill other processes. You
can't run external programs such as ssh as Python threads. Ending
a Python thread has essentially nothing to do with it.
There's more going on than ssh here. Since I want to run multiple
processes to multiple devices at one time and still have mass shared
memory I need to use threads. There's a mass distributed system that
needs to be controlled, that's the problem I'm trying to solve. You can
think of each ssh as a lengthy IO process that each gets its own
device. I use the threads to allow me to do IO to multiple devices at
once, ssh just happens to be the IO. The combination of threads and ssh
allowed us to have a *primitive* distributed system (and it works too,
so I *can* run external programs in python threads). I didn't say is
was the best or the correct solution, but it works and its what I was
handed when I was thrown into this project. I'm hoping in fifteen years
or when I get an army of monkeys to fix it, it will change. I'm not
worried about killing processes, that's easy, I could kill all the sshs
or whatever else I want without batting an eye. The threads that were
created in order to allow me to do all of this work simultaneously,
that's the issue. Granted, I'm heavily looking into a way of doing this
with processes, I still don't see how threads are the wrong choice with
my present situation.
>
Not me. I'm saying work the problem you actually have.
The problem I have is a large distributed system, that's the reality of
it. The short summary, I need to use and control 100+ machines in a
computing farm. They all need to share memory or to actively
communicate with each other via some other mechanism. Without giving
any other details, that's the problem I have to solve. Right now I'm
working with someone else's code. Without redesigning the system from
the ground up, I have to fix it.
--

Carl J. Van Arsdall
cv*********@mvi sta.com
Build and Release
MontaVista Software

Jul 26 '06 #19
"Carl J. Van Arsdall" <cv*********@mv ista.comwrites:
The problem I have is a large distributed system, that's the reality
of it. The short summary, I need to use and control 100+ machines in
a computing farm. They all need to share memory or to actively
communicate with each other via some other mechanism. Without giving
any other details, that's the problem I have to solve.
Have you looked at POSH yet? http://poshmodule.sf.net

There's also an shm module that's older and maybe more reliable.
Or you might be able to just use mmap.
Jul 26 '06 #20

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

Similar topics

2
2422
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
13
3497
by: Andy Fish | last post by:
Hi, I am using COM interop to invoke MS word from my .net app. If word pops up a dialog box, my thread obviously hangs until the dialog is answered. Here's the problem: because the app is running on a server, there is nobody there to answer the dialog, so I have another thread that calls Abort() on the first thread in the event that the operation doesn't complete within a certain amount of time.
16
6745
by: LP | last post by:
Hi, Considering code below. Will it make GC to actually collect. One application creates new instances of a class from 3rd party assembly in a loop (it has to). That class doesn't have .Dispose or any similar method. I want to make sure GC keeps up with the loop. My reasoning if Thread.Sleep(1000) is called; GC will take priority it do its work, right? GC.Collect(); GC.WaitForPendingFinalizers(); System.Threading.Thread.Sleep(1000);
4
2566
by: fred | last post by:
I use a Synclock in a secondary thread and also stop the thread using the abort method. If the abort occurs while the thread is in the Synclock will the SyncLock always be released before the thread stops or do I need to add some extra code to avoid Synclock staying on after the thread has been stopped. Thanks Fred
4
7022
by: kaiteriteri | last post by:
I have a time-consuming VB.net application that i'd like to thread over 2 processors (that's all i've got in my machine!) and, hopefully, get it done in half the time. On running, the application should create a 2nd thread and run it on the other processor (processing a distinct set of data), leaving the current thread to run and process its set of data. But the 2nd thread must run on the free processor, otherwise there's no point... ...
6
5997
by: Joe Jax | last post by:
I have an object that spawns a worker thread to process one of its methods. That method processes methods on a collection of other objects. During this processing, a user may request to cancel the entire operation. I could request abort on the worker thread, but that is a) potentially messy, and b) not guaranteed to take immediate effect anyway. I would rather have some way of allowing the main thread to tell the worker thread that it...
5
2808
by: Alan T | last post by:
I will do several things in my thread: Copy a file to a location Update database record Read the file content Write the content to a log file If I call Thread.Abort(), it may be possible to stop at the middle of the something ?
3
3186
by: yeye.yang | last post by:
hey everybody Does everybody can help me or give me some advise for the cross thread exception catch Here is what I want to do: I have 2 classes "Scenario" and "Step", which have a System.Thread.Timer for each to control their timeout gestion, in my "main program", I start the "Scenario" and "Step", and I do something between "StepStart" and "StepStop", when "Scenario" or "Step" timeout expired, they raise an "exception", then my "main...
1
2541
by: raghudr | last post by:
Hi all, I am displaying a splash screen for which i have created a thread.Since my whole project is launched by windows service and that service will start automatically at the start of the PC and sometimes when i start or restart the PC i have observed that even though the long process is completed splash
0
9860
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
9699
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
10922
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
10603
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...
1
10660
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,...
0
9440
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
7838
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...
2
4076
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3138
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.