Killing a thread
Carl J. Van Arsdall
cvanarsdall at mvista.com
Fri Jun 9 20:08:42 EDT 2006
Felipe Almeida Lessa wrote:
> Em Sex, 2006-06-09 às 13:54 -0700, Manish Marathe escreveu:
>
>> On 6/9/06, Fredrik Lundh <fredrik at pythonware.com> wrote:
>> Manish Marathe wrote:
>>
>> > I am creating threads using my self defined class which
>> inherits the
>> > threading.Thread class. I want to know how can I kill the
>> threads which
>> > are being created by the object of my self defined class.
>>
>> you cannot kill a thread "from the outside"; you have to
>> design your
>> thread tasks so they can kill themselves, when asked to do
>> that.
>>
>> Thanks for the reply. So can a thread listen to an event i.e. can we
>> send an event to the thread indicating to kill itself.
>>
>
> A plain simple boolean flag will certainly do the job. For example
>
> def run(self):
> self.running = True
> while self.running:
> blah()
>
> def stop(self):
> self.running = False
>
>
Well, this works if your threads are able to poll. If you had a thread
doing lengthy IO you could end up waiting a long time until the thread
gets an opportunity to kill itself.
Are there any plans in the future to add the capability to kill threads
from the outside? Better yet, an interruptable thread so instead of
using a polling loop you could send a DIE_THREAD_DIE signal or
something. I think at present its not possible (or a really bad idea)
to put signal handlers in threads. Anyone have thoughts on this?
I toyed with another idea (this is a big problem for me), but I noticed
that each python thread spawns a new interpreter. If python is doing
this, I would think that each thread could be associated with PIDs or
something. I haven't thought about it too much, its a little to
crazy/brute force for me, but I thought i'd throw it out there so you
guys could tell me if that one is a little too far fetched.
-carl
--
Carl J. Van Arsdall
cvanarsdall at mvista.com
Build and Release
MontaVista Software
More information about the Python-list
mailing list