Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

safest way to kill a thread

1 view
Skip to first unread message

martin...@excite.com

unread,
Jan 18, 2005, 10:48:17 PM1/18/05
to
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

limodou

unread,
Jan 18, 2005, 11:31:11 PM1/18/05
to martin...@excite.com, python-list
Using Thread's method setDaemon() before you call the start() method.
Just like :

t.setDaemon(True)
t.start()

--
I love python!
My Blog: http://www.donews.net/limodou

martin...@excite.com

unread,
Jan 19, 2005, 1:12:39 AM1/19/05
to
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.

limodou

unread,
Jan 19, 2005, 2:46:36 AM1/19/05
to martin...@excite.com, python-list
I think only those threads which invoked with setDaemon() method will
exit, and others will not, as the main program exit.

--

martin...@excite.com

unread,
Jan 19, 2005, 3:04:42 AM1/19/05
to
Great thank for your helping.
Should the 'daemonic' flag at setDaemon() function set to 1/TRUE or
0/FALSE to do such action?

hoxide

unread,
Jan 19, 2005, 7:10:16 AM1/19/05
to
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 .

Peter Hansen

unread,
Jan 19, 2005, 9:36:59 AM1/19/05
to
martin...@excite.com 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

martin...@excite.com

unread,
Jan 19, 2005, 9:40:13 PM1/19/05
to
Thank for all helping and sorry that i overlooked the previous
message.
0 new messages