473,395 Members | 2,010 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,395 software developers and data experts.

How to kill threading.Thread instance?

hi all,
Is there a better way to kill threading.Thread (running) instance than
this one
http://aspn.activestate.com/ASPN/Coo.../Recipe/496960
(it's all I have found via google).

BTW, it should be noticed that lots of threading module methods have
no docstrings (in my Python 2.5), for example _Thread__bootstrap,
_Thread__stop.

Regards, D.
Sep 21 '08 #1
7 6117
dmitrey schrieb:
hi all,
Is there a better way to kill threading.Thread (running) instance than
this one
http://aspn.activestate.com/ASPN/Coo.../Recipe/496960
(it's all I have found via google).
Nope. There might be other ways technically, but none of them falls into
a "better"-classification.

Diez
Sep 21 '08 #2
dmitrey wrote:
BTW, it should be noticed that lots of threading module methods have
no docstrings (in my Python 2.5), for example _Thread__bootstrap,
_Thread__stop.
things named _Class__name are explicitly marked private by the
implementation (using the "__" prefix).

using them just because you can find them via "dir" is a really stupid
idea. (and, as noted in the comment section to the recipe, the "stop"
method flags a thread as stopped, it doesn't stop it.)

</F>

Sep 21 '08 #3
I wonder why something like myThread.exit() or myThread.quit() or
threading.kill(myThread) can't be implemented?
Is something like that present in Python 3000?
Regards, D.
Sep 21 '08 #4
dmitrey schrieb:
I wonder why something like myThread.exit() or myThread.quit() or
threading.kill(myThread) can't be implemented?
Is something like that present in Python 3000?
Not that I'm aware of it (which doesn't mean to much though).

However I *am* aware of the bazillions discussions that have been held
over this here - and the short answer is: it is a generally very bad
idea to terminate threads hard, as it can cause all kinds of corruption.

Systems like Java discourage the use of the available methods for that
as well. And I for example once worked with Qt3-threads, what allow for
this kind of operation - and killed my CORBA-ORB running in the same
process by terminating the thread hard.

Google a bit in this NG to find the discussions & reasons.

Diez
Sep 21 '08 #5
Diez B. Roggisch wrote:
>I wonder why something like myThread.exit() or myThread.quit() or
threading.kill(myThread) can't be implemented?
Is something like that present in Python 3000?

Not that I'm aware of it (which doesn't mean to much though).

However I *am* aware of the bazillions discussions that have been held
over this here - and the short answer is: it is a generally very bad
idea to terminate threads hard, as it can cause all kinds of corruption.
the problem is that you have no idea what the thread is doing, so just
killing it dead it may make one big mess out of the application's
internal state; see e.g. this post

http://mail.python.org/pipermail/pyt...st/400256.html

That's wise ;-) Stopping a thread asynchronously is in /general/ a
dangerous thing to do, and for obvious reasons. For example, perhaps
the victim thread is running in a library routine at the time the
asynch exception is raised, and getting forcibly ejected from the
normal control flow leaves a library-internal mutex locked forever.
Or perhaps a catch-all "finally:" clause in the library manages to
release the mutex, but leaves the internals in an inconsistent state.

which links to a FAQ from Sun on this very topic:

http://java.sun.com/j2se/1.3/docs/gu...precation.html

(note that Java releases all mutexes when a thread is killed, but that's
not much better, as the FAQ explains)

so as usual, the right thing to do is to do things in the right way.

</F>

Sep 21 '08 #6
On Sep 21, 4:04*pm, Fredrik Lundh <fred...@pythonware.comwrote:
Diez B. Roggisch wrote:
I wonder why something like myThread.exit() or myThread.quit() or
threading.kill(myThread) can't be implemented?
Is something like that present in Python 3000?
Not that I'm aware of it (which doesn't mean to much though).
However I *am* aware of the bazillions discussions that have been held
over this here - and the short answer is: it is a generally very bad
idea to terminate threads hard, as it can cause all kinds of corruption..

the problem is that you have no idea what the thread is doing, so just
killing it dead it may make one big mess out of the application's
internal state; see e.g. this post

* *http://mail.python.org/pipermail/pyt...st/400256.html

* *That's wise ;-) *Stopping a thread asynchronously is in /general/ a
* *dangerous thing to do, and for obvious reasons. *For example, perhaps
* *the victim thread is running in a library routine at the time the
* *asynch exception is raised, and getting forcibly ejected from the
* *normal control flow leaves a library-internal mutex locked forever..
* *Or perhaps a catch-all "finally:" clause in the library manages to
* *release the mutex, but leaves the internals in an inconsistent state.

which links to a FAQ from Sun on this very topic:

http://java.sun.com/j2se/1.3/docs/gu...itiveDeprecati...

(note that Java releases all mutexes when a thread is killed, but that's
not much better, as the FAQ explains)

so as usual, the right thing to do is to do things in the right way.

</F>
Often you know terminated a thread would be perfectly safe - and not
being able to is very frustrating - particularly if your calculation
is coarse grained and there is no convenient point to regularly poll
for a stop signal.

..NET solves the 'you might interrupt important stuff' by guaranteeing
that an asynchronous ThreadAbortException won't be raised inside a
finally block.

Michael
http://www.ironpythoninaction.com/
Sep 21 '08 #7
On 2008-09-21, Fredrik Lundh <fr*****@pythonware.comwrote:
Diez B. Roggisch wrote:
>>I wonder why something like myThread.exit() or myThread.quit() or
threading.kill(myThread) can't be implemented?
Is something like that present in Python 3000?

Not that I'm aware of it (which doesn't mean to much though).

However I *am* aware of the bazillions discussions that have been held
over this here - and the short answer is: it is a generally very bad
idea to terminate threads hard, as it can cause all kinds of corruption.

the problem is that you have no idea what the thread is doing, so just
killing it dead it may make one big mess out of the application's
internal state; see e.g. this post

http://mail.python.org/pipermail/pyt...st/400256.html

That's wise ;-) Stopping a thread asynchronously is in /general/ a
dangerous thing to do, and for obvious reasons. For example, perhaps
the victim thread is running in a library routine at the time the
asynch exception is raised, and getting forcibly ejected from the
normal control flow leaves a library-internal mutex locked forever.
Or perhaps a catch-all "finally:" clause in the library manages to
release the mutex, but leaves the internals in an inconsistent state.

which links to a FAQ from Sun on this very topic:

http://java.sun.com/j2se/1.3/docs/gu...precation.html

(note that Java releases all mutexes when a thread is killed, but that's
not much better, as the FAQ explains)

so as usual, the right thing to do is to do things in the right way.
Why not let the programmer make the call whether killing the thread dead
is the right thing or not. Maybe the programmer has a pretty good idea
about what the thread can possibilbly be doing and knows that killing it
won't produce a mess.

Sure caution people to be very carefull when they are thinking about
doing something like this. Just as people are generally adviced to
use Queues when doing multithreading. But that is no reason to
disallow certain kind of actions.

--
Antoon Pardon
Sep 26 '08 #8

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

Similar topics

12
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...
77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
6
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...
0
by: mumebuhi | last post by:
I removed my previous post about this topic because I apparently have pasted the wrong code. Sorry for the confusion and thanks for being patient. I am having problem to kill the following...
5
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...
1
by: spectrumdt | last post by:
Thanks for the replies. On May 8, 5:50 pm, Jean-Paul Calderone <exar...@divmod.comwrote: Yes, thank you, this seems to work. :) I did some more testing and found out that the problem seems...
20
by: =?ISO-8859-1?Q?Gerhard_H=E4ring?= | last post by:
John Dohn wrote: 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...
6
by: George Sakkis | last post by:
I'm baffled with a situation that involves: 1) an instance of some class that defines __del__, 2) a thread which is created, started and referenced by that instance, and 3) a weakref proxy to the...
4
by: Mathieu Prevot | last post by:
Hi, I have a threading.Thread class with a "for i in range(1,50)" loop within. When it runs and I do ^C, I have the error as many as loops. I would like to catch this exception (and if possible...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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,...
0
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...
0
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,...

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.