How to kill a thread from another thread ? 
Author Message
 How to kill a thread from another thread ?

Hi Pythoners,

Does someone know how to kill a python thread from another Python thread (in the
same process) ?
In both thread and threading modules, there is functions to launch such threads,
but no way to kill/terminate them. On WIN32, the WIN32 function
TerminateThread() is missing in the  Win32 extensions' win32 api module.

Thanks for any help,

Richard Gruet



Mon, 28 Apr 2003 03:00:00 GMT  
 How to kill a thread from another thread ?

 | Does someone know how to kill a Python thread from another Python
 | thread

you cannot.

  -- erno



Mon, 28 Apr 2003 03:00:00 GMT  
 How to kill a thread from another thread ?

I've only done fairly limited stuff with threads, but I believe you use
the pid of the target thread, and do something like os.kill(pid).  The
pid is returned to the parent when a child thread is forked off.

Alex.

--
Speak softly but carry a big carrot.



Mon, 28 Apr 2003 03:00:00 GMT  
 How to kill a thread from another thread ?
You can also do this with an Event trigger (each thread periodically checks
and shuts itself down) or with a common Queue (an item in it acting as a
trigger).

HTH,

Jose


Quote:
> Hi Pythoners,

> Does someone know how to kill a Python thread from another Python thread
(in the
> same process) ?
> In both thread and threading modules, there is functions to launch such
threads,
> but no way to kill/terminate them. On WIN32, the WIN32 function
> TerminateThread() is missing in the  Win32 extensions' win32 api module.

> Thanks for any help,

> Richard Gruet



Mon, 28 Apr 2003 03:00:00 GMT  
 How to kill a thread from another thread ?

Quote:

>Does someone know how to kill a Python thread from another Python
>thread (in the same process) ?

You don't.  You need to set up each of your threads so that they look
for some kind of signal to kill themselves (which means returning).  It
has been argued that the ability to kill threads is necessary for server
applications, but I haven't seen any powerful arguments in favor of that.
--

Androgynous poly {*filter*} vanilla {*filter*} het    <*>     http://www.*-*-*.com/
Hugs and backrubs -- I break Rule 6

"Every flame is sacred, every flame is great."  --Orc



Mon, 28 Apr 2003 03:00:00 GMT  
 How to kill a thread from another thread ?

Quote:


> >Does someone know how to kill a Python thread from another Python
> >thread (in the same process) ?

> You don't.  You need to set up each of your threads so that they look
> for some kind of signal to kill themselves (which means returning).  It
> has been argued that the ability to kill threads is necessary for server
> applications, but I haven't seen any powerful arguments in favor of that.

Actually, I'm using a thread to execute a function call asynchronously, to avoid
getting stuck is something goes wrong. To execute the call, I create a thread and
delegate the actual function call to it. Then, in the main thread, I wait for the
end of the thread (ie the return of the function called) with a timeout (using a
join).
The problem is that if a timeout occurs because the function doesn't return, I
have no way to kill the thread and sometimes it appears to prevent the program to
exit correctly (it hangs because there is still living threads).

Richard



Mon, 28 Apr 2003 03:00:00 GMT  
 How to kill a thread from another thread ?
Lets see if there are other alternatives then :-) Are you using
sockets? Put them in non-blocking mode, get in touch with me to get my
latest socket module.. :-)

Another alternative is if you only need to run one thread, and on a UNIX system, you can get a sigalarm sent to you..

        /Andy



Tue, 29 Apr 2003 03:00:00 GMT  
 How to kill a thread from another thread ?

Quote:



>>Does someone know how to kill a Python thread from another Python
>>thread (in the same process) ?

>You don't.  You need to set up each of your threads so that they
>look for some kind of signal to kill themselves (which means
>returning).  It has been argued that the ability to kill threads is
>necessary for server applications, but I haven't seen any powerful
>arguments in favor of that.

How about doing blocking I/O in a thread? If something goes wrong the
thread might block forever. Anticipating the response: "So don't do
blocking I/O", I'd like to add that you may not have that option,
because the blocking call could be in another module which you need to
use but didn't write yourself.

If Python uses non-blocking I/O internally, *and* it had the ability to
raise exceptions in another thread you would have a very powerful
mechanism to kill threads and still have a thread clean up after itself
(which is, I think, the most powerful argument against having threads
kill each other).

Robert Amesz



Wed, 30 Apr 2003 10:12:52 GMT  
 How to kill a thread from another thread ?

Quote:




> >>Does someone know how to kill a Python thread from another Python
> >>thread (in the same process) ?

> >You don't.  You need to set up each of your threads so that they
> >look for some kind of signal to kill themselves (which means
> >returning).  It has been argued that the ability to kill threads is
> >necessary for server applications, but I haven't seen any powerful
> >arguments in favor of that.

> How about doing blocking I/O in a thread? If something goes wrong the
> thread might block forever. Anticipating the response: "So don't do
> blocking I/O", I'd like to add that you may not have that option,
> because the blocking call could be in another module which you need to
> use but didn't write yourself
> If Python uses non-blocking I/O internally, *and* it had the ability to
> raise exceptions in another thread you would have a very powerful
> mechanism to kill threads and still have a thread clean up after itself
> (which is, I think, the most powerful argument against having threads
> kill each other).

In my particular case, I want to kill the thread from the thread which created
it when something is going wrong (e.g. a timed out I/O) and furthermore I
performed the cautious operation in a separate thread precisely to be avoid
getting stuck in case of problem.
To me this is a good reason to want to kill a thread in this case. I agree that
*usually* killing a thread without clean-up is not a good policy!

This leads me back to my original question: how can I do that (and in a portable
way) ? Apparently there is no obvious solution.
To use a separate process to execute an operation is quite inefficient (although
in this case I know how to kill the process).

Richard Gruet



Wed, 30 Apr 2003 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Thread#kill doesn't kill processes inside a thread

2. threading: how to kill a (possibly blocked) thread?

3. threads killing other threads

4. Threads creating threads creating threads...

5. thread, threading, mutex modules and non-threading interpreters

6. How can you kill a thread ???

7. Vispro Rexx can't kill a rxftp thread

8. Is there a REXX Kill Thread Function?

9. killing a thread and its child

10. Kill this thread.

11. Killing a running thread

12. Sockets, threads, and killing servers

 

 
Powered by phpBB® Forum Software