[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: Killing worker threads

Fredrik Lundh

1/6/2008 1:49:00 PM

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>

1 Answer

Mike Driscoll

1/7/2008 4:20:00 PM

0

On Jan 6, 7:48 am, Fredrik Lundh <fred...@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-thre...

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

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