473,666 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: How to kill a thread?

John Dohn wrote:
Hi there,

How can I kill a threading.Threa d subclass from MainThread?

My threads are waiting for data in Queue.get() method:
class MyThread(thread ing.Thread):
def run(self):
while True:
data = queue.get() # <- here it waits most of the time
... process data

From the main thread I start several working threads:
thr = MyThread()
thr.start()
... feed the queue
and at the end for each thread I'd like to do something like thr.kill()
or thr.stop() or thr.destroy() or ... you got the point. I can't figure
out how.

Is there a way to do it?
When I do this, I put a special value in the queue (like None) and in
the worker thread, check for the special value and exit if found.

Threads can also be marked as "daemon threads" (see docs for
threading.Threa d objects). This will make the application terminate if
only "daemon threads" are left.

So best would probably be soemthing like

- call setDaemon() when creating worker threads
- ...
- send all worker/daemon threads None via queue
- wait some time, like time.sleep(1.0) , to let daemon exit themselves
- exit main application

-- Gerhard

Jun 27 '08 #1
20 5068
It's always been my understanding that you can't forcibly kill a thread
in Python (at least not in a portable way). The best you can do is
politely ask it to die, IIRC.

--
code.py: A blog about life, the universe, and Python

http://pythonista.wordpress.com
** Posted from http://www.teranews.com **
Jun 27 '08 #2
On Jun 6, 12:44 pm, The Pythonista <n...@this.time wrote:
It's always been my understanding that you can't forcibly kill a thread
in Python (at least not in a portable way). The best you can do is
politely ask it to die, IIRC.
Inherently, the best you can do in most languages is ask them politely
to die. Otherwise you'll leave locks and various other datastructures
in an inconvenient state, which is too complex to handle correctly.
The exception is certain functional languages, which aren't capable of
having threads and complex state in the same sense.

However, you could go quite far with a standardized mechanism of
politely asking threads to die. For instance, all blocking I/O
functions could be made to support it. This is how cancellation works
in python-safethread.
Jun 27 '08 #3
Would it be possible for a 3rd-party threading library to an
'interruptible' version of Queue?

The same methods as the normal Queue, but when you call a new .kill()
method on the queue, all the threads locking on the queue get a
"QueueKille d" exception thrown.

It might be as simple as a Queue wrapper where .kill() adds a speciall
Killed value to the queue, and the .get() wrapper throws a QueueKilled
exception (after re-putting Killed) when it reads Killed.

Locking for queues with a limited size is more complicated. Maybe
..kill() can also set an .killed attribute, and pop anything already in
the queue before putting Killed. Then when the put() wrapper unlocks,
it checks if .killed is set, and throws a QueueKilled exception.

One side-effect is that the queue becomes unusable (get & put now
immediately throw QueueKilled), unless you make a .unkill() method.

David
Jun 27 '08 #4
David <wi******@gmail .comwrote:
Would it be possible for a 3rd-party threading library to an
'interruptible' version of Queue?

The same methods as the normal Queue, but when you call a new .kill()
method on the queue, all the threads locking on the queue get a
"QueueKille d" exception thrown.

It might be as simple as a Queue wrapper where .kill() adds a speciall
Killed value to the queue, and the .get() wrapper throws a QueueKilled
exception (after re-putting Killed) when it reads Killed.
There seem to me to be 2 basic types of threads - I'll call them
client threads and server threads.

A client thread listens on a Queue for its instructions and replies
via a different Queue, or via Queues passed in in its instructions.
That kind of thread can easily have an instruction which means quit.
When it isn't processing instructions there is a nice clean moment for
it to quit. However if the processing takes a long time then there
will be a long wait for the thread to die. This is the scenario
described above.

The server type thread is processing all the time. Every now and
again it sends its results via a Queue. An example of this type of
thread might be one listening on a socket. This type of thread has no
nice moment to listen for instructions to quit as it might well be
blocked on something else (eg listening to a socket). To make this
type of thread kill nicely you have to go back to breaking up your
work into chunks (eg polling the socket asynchronously) and polling
the command queue to wait for your quit instruction which is wastefull
of CPU resources.

So it seems to me that there isn't a good solution to killing python
threads of either type, other than coding them carefully and checking
for quit instructions regularly.

You can kill them using the internal python API and ctypes, but you
run the risk of leaving things in an inconsistent state which is why
that API isn't exposed.

Here is an attempt at a killable thread

http://aspn.activestate.com/ASPN/Coo.../Recipe/496960

and

http://sebulba.wikispaces.com/recipe+thread2

Read the caveats!

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jun 27 '08 #5
On 2008-06-07, Rhamphoryncus <rh****@gmail.c omwrote:
On Jun 6, 12:44 pm, The Pythonista <n...@this.time wrote:
>It's always been my understanding that you can't forcibly kill a thread
in Python (at least not in a portable way). The best you can do is
politely ask it to die, IIRC.

Inherently, the best you can do in most languages is ask them politely
to die. Otherwise you'll leave locks and various other datastructures
in an inconvenient state, which is too complex to handle correctly.
The exception is certain functional languages, which aren't capable of
having threads and complex state in the same sense.
Well it would of course depend on what is considered asking politely?

If one thread could cause an exception being thrown in an other thread,
would this be considered a polite way to ask? Would it be considered
an acceptable way?

--
Antoon Pardon
Jun 27 '08 #6
On 2008-06-07, David <wi******@gmail .comwrote:
Would it be possible for a 3rd-party threading library to an
'interruptible' version of Queue?

The same methods as the normal Queue, but when you call a new .kill()
method on the queue, all the threads locking on the queue get a
"QueueKille d" exception thrown.
I have done something similar. The idea was that threads had to open
a queue before they could access it. If the last thread who had the
queue open in write mode, closed the queue, a reader trying to get
an element from an empty queue would have an "EOInformat ion" exception
raised.

--
Antoon Pardon
Jun 27 '08 #7
On Jun 9, 5:33 am, Antoon Pardon <apar...@forel. vub.ac.bewrote:
On 2008-06-07, Rhamphoryncus <rha...@gmail.c omwrote:
On Jun 6, 12:44 pm, The Pythonista <n...@this.time wrote:
It's always been my understanding that you can't forcibly kill a thread
in Python (at least not in a portable way). The best you can do is
politely ask it to die, IIRC.
Inherently, the best you can do in most languages is ask them politely
to die. Otherwise you'll leave locks and various other datastructures
in an inconvenient state, which is too complex to handle correctly.
The exception is certain functional languages, which aren't capable of
having threads and complex state in the same sense.

Well it would of course depend on what is considered asking politely?

If one thread could cause an exception being thrown in an other thread,
would this be considered a polite way to ask? Would it be considered
an acceptable way?
The exception must not be raised until a point explicitly designed as
safe is hit. Otherwise, any function that manipulates data you'll
still use will potentially be buggered. Consider sys.stdout: codecs,
buffering, lots to go wrong.
Jun 27 '08 #8
On Jun 9, 9:20*pm, Rhamphoryncus <rha...@gmail.c omwrote:
On Jun 9, 5:33 am, Antoon Pardon <apar...@forel. vub.ac.bewrote:
On 2008-06-07, Rhamphoryncus <rha...@gmail.c omwrote:
On Jun 6, 12:44 pm, The Pythonista <n...@this.time wrote:
>It's always been my understanding that you can't forcibly kill a thread
>in Python (at least not in a portable way). *The best you can do is
>politely ask it to die, IIRC.
Inherently, the best you can do in most languages is ask them politely
to die. *Otherwise you'll leave locks and various other datastructures
in an inconvenient state, which is too complex to handle correctly.
The exception is certain functional languages, which aren't capable of
having threads and complex state in the same sense.
Well it would of course depend on what is considered asking politely?
If one thread could cause an exception being thrown in an other thread,
would this be considered a polite way to ask? Would it be considered
an acceptable way?

The exception must not be raised until a point explicitly designed as
safe is hit. *Otherwise, any function that manipulates data you'll
still use will potentially be buggered. *Consider sys.stdout: codecs,
buffering, lots to go wrong.

Java and .NET both have ways of killing threads. They both work by
raising a 'ThreadAbort' (or similar) exception in the target thread.
In early implementations they both suffered from a similar problem.
You could protect locks (etc) by having a finally block that would
release all resources as needed - but what happens if the thread abort
exception is raised *inside* the finally block?

Java responded by deprecating thread aborting. .NET responded by
ensuring that a thread abort exception would never be raised inside a
finally block - if that happened the exception would only be raised
once the code has left the finally block.

Aborting threads in .NET can be extremely useful. Politely asking a
thread to die is no good if the task the thread is executing is
extremely coarse grained - it may not be able to respond to the
request for some time. If your code is structured correctly
(performing a long running background calculation for example) then
you may *know* that you can kill it without problems, but Python has
no native API to do this.

Michael Foord
http://www.ironpythoninaction.com/
Jun 27 '08 #9
On Jun 9, 2:52 pm, Fuzzyman <fuzzy...@gmail .comwrote:
On Jun 9, 9:20 pm, Rhamphoryncus <rha...@gmail.c omwrote:
On Jun 9, 5:33 am, Antoon Pardon <apar...@forel. vub.ac.bewrote:
On 2008-06-07, Rhamphoryncus <rha...@gmail.c omwrote:
On Jun 6, 12:44 pm, The Pythonista <n...@this.time wrote:
It's always been my understanding that you can't forcibly kill a thread
in Python (at least not in a portable way). The best you can do is
politely ask it to die, IIRC.
Inherently, the best you can do in most languages is ask them politely
to die. Otherwise you'll leave locks and various other datastructures
in an inconvenient state, which is too complex to handle correctly.
The exception is certain functional languages, which aren't capable of
having threads and complex state in the same sense.
Well it would of course depend on what is considered asking politely?
If one thread could cause an exception being thrown in an other thread,
would this be considered a polite way to ask? Would it be considered
an acceptable way?
The exception must not be raised until a point explicitly designed as
safe is hit. Otherwise, any function that manipulates data you'll
still use will potentially be buggered. Consider sys.stdout: codecs,
buffering, lots to go wrong.

Java and .NET both have ways of killing threads. They both work by
raising a 'ThreadAbort' (or similar) exception in the target thread.
In early implementations they both suffered from a similar problem.
You could protect locks (etc) by having a finally block that would
release all resources as needed - but what happens if the thread abort
exception is raised *inside* the finally block?

Java responded by deprecating thread aborting. .NET responded by
ensuring that a thread abort exception would never be raised inside a
finally block - if that happened the exception would only be raised
once the code has left the finally block.

Aborting threads in .NET can be extremely useful. Politely asking a
thread to die is no good if the task the thread is executing is
extremely coarse grained - it may not be able to respond to the
request for some time. If your code is structured correctly
(performing a long running background calculation for example) then
you may *know* that you can kill it without problems, but Python has
no native API to do this.
So how does .NET deal with the sys.stdout corruption? Does it?

If you've carefully written your code to use only safe primitives and
local state (discarded if interrupted) then yes, it could be
interruptible. At this point you could specially mark that block of
code as safe, leaving the vast majority of other code unsafe by
default. Then again, since you're going to the trouble of carefully
designing and auditing your code you could just make it cancellable
like blocking I/O should be - just by polling a flag at key points
(and you're CPU-bound anyway, so it's not expensive.)

The only place I know of that you *need* arbitrary interruption is
hitting CTRL-C in the interactive interpreter. At this point it's a
debugging tool though, so the risk of weirdness is acceptable.
Jun 27 '08 #10

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

Similar topics

12
18885
by: Jerry Sievers | last post by:
Greetings Pythonists; I have limited experience with threaded apps and plenty with old style forked heavyweight multi-processing apps. Using Python 2.3.3 on a Redhat 7.x machine. Wondering if there is a simple way from a main python program to kill a running thread? I see with the 'threading' module the way daemonic threads behave when the main program finishes.
5
10924
by: Blatwurst | last post by:
I'm trying to implement a simple server in C#. I want to do the classic thing of spinning off a thread that just blocks in a Socket.Accept() call until a request comes in. At that point, the Accept() returns, the thread spins off another thread to handle the request, and then calls Accept() again. This all works fine except that I can find no way to kill the thread that is blocked in the Accept() call when I want to shut down the server. ...
6
42871
by: RickDee | last post by:
Understand that when I start a thread, a number will be generated and is able to get from GetHashCode method. But I would like to use this number when I want to kill certain thread, anybody know how ?? Thanks Regards
3
5842
by: Stewart | last post by:
Hey Group, Hoping someone can help me out. I have some code which starts up some asynchronous code using a delegate. The code is below. Basically my main code (not shown) calls ServerThreadStart.StartServer to start the server running asynchronously. This works fine. Shouldn't be any problems here. My question is how can I get my code to kill this code running asynchronously? There is a dlgtServer.Remove(Delegate, Delegate)
9
15273
by: Brett | last post by:
I'm trying to kill a thread spawned this way: Form1 spawns Class1 via Thread.start() Here's my code to kill the thread: If (t.ThreadState.ToString = "SuspendedRequested, WaitSleepJoin") Or (t.ThreadState.ToString = "Suspended") Or (t.ThreadState.ToString = "WaitSleepJoin, Suspended") Then Else t.Abort()
2
3496
by: Christopher Carnahan | last post by:
I need to figure out how to terminate a thread while it is blocked trying to create a COM object via interop. In a worker thread, I do something like this: Type t = null; Object activatedObject = null; Legacy.IScheduled comObject = null; t = Type.GetTypeFromProgID(ProgID);
5
10578
by: care02 | last post by:
Hi! I have the following problem: I have written a short Python server that creates an indefinite simulation thread that I want to kill when quitting (Ctrl-C) from Python. Googling around has not given me any hints on how to cleanly kill running threads before exiting. Any help is appreciated! Carl
18
10225
by: =?Utf-8?B?VGhlU2lsdmVySGFtbWVy?= | last post by:
Because C# has no native SSH class, I am using SharpSSH. Sometimes, for reasons I do not know, a Connect call will totally lock up the thread and never return. I am sure it has something to do with weirdness going on with the server I am talking to. Anyhow, this locked up state happens once in a while (maybe once per day) and I can't figure out how to deal with the locked up thread. If I issue a Thread.Abort() the exception never...
1
10379
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post by:
Hi misters, Is it possible "kill" the thread of Backgroundworker ? In my Dowork event, I have NOT While for do e.Cancel = true, only have a call to external COM. If I want cancel, calling CancelAsync, not cancels the call to COM. How I can do it , please ? Any suggestions will be very appreciated.
0
8866
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
8781
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
8639
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...
1
6192
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
5663
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
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...
1
2769
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
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1772
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.