Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > safest way to kill a thread

Reply
Thread Tools

safest way to kill a thread

 
 
martinnitram@excite.com
Guest
Posts: n/a
 
      01-19-2005
Dear all,
in python, a thread can be created by t = threading.Thread. But i
found that when the main (and the thread) program is running and user
use Crtl+C/Crtl+Z to break the program abnormally, the thread is still
running and needed to kill manually (by the pid). Is there had any
safest way to kill/exit the thread program under python (when the
thread program part is a forever loop)?

Thank a lot

 
Reply With Quote
 
 
 
 
limodou
Guest
Posts: n/a
 
      01-19-2005
Using Thread's method setDaemon() before you call the start() method.
Just like :

t.setDaemon(True)
t.start()

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Dear all,
> in python, a thread can be created by t = threading.Thread. But i
> found that when the main (and the thread) program is running and user
> use Crtl+C/Crtl+Z to break the program abnormally, the thread is still
> running and needed to kill manually (by the pid). Is there had any
> safest way to kill/exit the thread program under python (when the
> thread program part is a forever loop)?
>
> Thank a lot
>


--
I love python!
My Blog: http://www.donews.net/limodou
 
Reply With Quote
 
 
 
 
martinnitram@excite.com
Guest
Posts: n/a
 
      01-19-2005
limodou wrote:
>Using Thread's method setDaemon() before you call the start() method.
>Just like :
>t.setDaemon(True)
>t.start()

thank for fast reply.
from python.org doc, said that setDaemon() function as
"The entire Python program exits when no active non-daemon threads
are left."
is it mean that when the main program exit (normally/abnormally), all
threads created will also exit?
Thank again.

 
Reply With Quote
 
limodou
Guest
Posts: n/a
 
      01-19-2005
I think only those threads which invoked with setDaemon() method will
exit, and others will not, as the main program exit.

(E-Mail Removed) wrote:
> limodou wrote:
>
>>Using Thread's method setDaemon() before you call the start() method.
>>Just like :
>>t.setDaemon(True)
>>t.start()

>
> thank for fast reply.
> from python.org doc, said that setDaemon() function as
> "The entire Python program exits when no active non-daemon threads
> are left."
> is it mean that when the main program exit (normally/abnormally), all
> threads created will also exit?
> Thank again.
>


--
I love python!
My Blog: http://www.donews.net/limodou
 
Reply With Quote
 
martinnitram@excite.com
Guest
Posts: n/a
 
      01-19-2005
Great thank for your helping.
Should the 'daemonic' flag at setDaemon() function set to 1/TRUE or
0/FALSE to do such action?

limodou wrote:
>I think only those threads which invoked with setDaemon() method will
>exit, and others will not, as the main program exit.


 
Reply With Quote
 
hoxide
Guest
Posts: n/a
 
      01-19-2005
To Catch the "SystemExit"

import thread
from time import sleep
import sys

def t1():
try:
i=0
while 1:
print i+1
i += 1
sleep(1)
except SystemExit:
pass

thread.start_new_thread(t1, ())
sleep(3)


This Question was also asked in Python-chinese .

 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      01-19-2005
(E-Mail Removed) wrote:
> Should the 'daemonic' flag at setDaemon() function set to 1/TRUE or
> 0/FALSE to do such action?


First of all, it's "True" and "False" in Python, not TRUE
and FALSE.

Secondly, the answer to the question was in the previous
message where "limodou" told you about this in the first
place. Go back and read it again...

-Peter
 
Reply With Quote
 
martinnitram@excite.com
Guest
Posts: n/a
 
      01-20-2005
Thank for all helping and sorry that i overlooked the previous
message.

Peter Hansen wrote:
> (E-Mail Removed) wrote:
> > Should the 'daemonic' flag at setDaemon() function set to 1/TRUE or
> > 0/FALSE to do such action?

>
> First of all, it's "True" and "False" in Python, not TRUE
> and FALSE.
>
> Secondly, the answer to the question was in the previous
> message where "limodou" told you about this in the first
> place. Go back and read it again...
>
> -Peter


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
the safest way of handling datetime between asp.net and ms sql H5N1 ASP .Net 0 10-09-2006 01:47 AM
Firefox, Mozilla, Netscape, not the safest way to go [STICKY] Uplink Computer Information 6 12-23-2005 07:14 PM
Safest Way to Share Files? =?Utf-8?B?THVpcw==?= Wireless Networking 3 11-21-2005 05:07 PM
safest way to open files on all platforms rbt Python 3 02-14-2005 02:46 PM
Safest way to clean up registry??? Bud Light Computer Support 7 12-26-2004 02:56 AM



Advertisments