Killing worker threads

kyosohma at gmail.com kyosohma at gmail.com
Mon Jan 7 11:20:19 EST 2008


On Jan 6, 7:48 am, Fredrik Lundh <fred... at pythonware.com> wrote:
> tarun wrote:
> > Can anyone help me with a simple code through which the main thread can
> > kill the worker thread it started.
>
> it cannot.  threads cannot be killed from the "outside".
>
> </F>

The only way to "kill" a thread is to have the spawned thread have
some kind of passed in argument which will trigger it to shut down.
You could have the thread read a file or file-like object periodically
(like a timer) and if it meets some condition, have the thread quit.

It's kind of like a subscription process. The thread subscribes to the
main program and can accept signals. I suggest that the OP read the
docs on threads:

http://docs.python.org/lib/module-threading.html

Of special interest to this user: http://docs.python.org/lib/condition-objects.html

I have messed with KillProcName, a PyWin32 script with limited
success. There's also the following snippet which is Windows only and
works for some processes and not others (i.e. unreliable):

subprocess.Popen('taskkill /s %s /im %s' % (computer_id, proc))

Hopefully I didn't muddy the waters or write something too off the
mark.

Mike



More information about the Python-list mailing list