Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > kill thread

Reply
Thread Tools

kill thread

 
 
Mathieu Prevot
Guest
Posts: n/a
 
      08-07-2008
Hi,

I have a threading.Thread class with a "for i in range(1,50)" loop
within. When it runs and I do ^C, I have the error [1] as many as
loops. I would like to catch this exception (and if possible do some
cleanup like in C pthreads) so the program finishes cleanly. Where and
how can I do this ? in __run__ ? __init__ ? a try/except stuff ?

Thanks,
Mathieu

[1]:
^CException in thread Thread-1:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/threading.py",
line 486, in __bootstrap_inner
self.run()
File "./youfetch.py", line 148, in run
self.getids()
File "./youfetch.py", line 145, in getids
self.ids.append(self.getidsatpage(i))
File "./youfetch.py", line 138, in getidsatpage
self.child = subprocess.Popen(cmd.split(),stdout=subprocess.PIP E)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/subprocess.py",
line 594, in __init__
errread, errwrite)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/subprocess.py",
line 1011, in _execute_child
self.pid = os.fork()
KeyboardInterrupt
 
Reply With Quote
 
 
 
 
Miki
Guest
Posts: n/a
 
      08-08-2008
Hello,

> I have a threading.Thread class with a "for i in range(1,50)" loop
> within. When it runs and I do ^C, I have the error [1] as many as
> loops. I would like to catch this exception (and if possible do some
> cleanup like in C pthreads) so the program finishes cleanly. Where and
> how can I do this ? in __run__ ? __init__ ? a try/except stuff ?

You can have a try/except KeyboardException around the thread code.

HTH,
--
Miki <(E-Mail Removed)>
http://pythonwise.blogspot.com
 
Reply With Quote
 
 
 
 
Mathieu Prevot
Guest
Posts: n/a
 
      08-08-2008
2008/8/8 Miki <(E-Mail Removed)>:
> Hello,
>
>> I have a threading.Thread class with a "for i in range(1,50)" loop
>> within. When it runs and I do ^C, I have the error [1] as many as
>> loops. I would like to catch this exception (and if possible do some
>> cleanup like in C pthreads) so the program finishes cleanly. Where and
>> how can I do this ? in __run__ ? __init__ ? a try/except stuff ?

> You can have a try/except KeyboardException around the thread code.
>
> HTH,
> --
> Miki


Of course, but I don't know where. I placed this inside loop, within
called functions from the loop, I still have the problem.

Mathieu
 
Reply With Quote
 
bockman@virgilio.it
Guest
Posts: n/a
 
      08-08-2008
On 8 Ago, 10:03, "Mathieu Prevot" <(E-Mail Removed)> wrote:
> 2008/8/8 Miki <(E-Mail Removed)>:
>
> > Hello,

>
> >> I have a threading.Thread class with a "for i in range(1,50)" loop
> >> within. When it runs and I do ^C, I have the error [1] as many as
> >> loops. I would like to catch this exception (and if possible do some
> >> cleanup like in C pthreads) so the program finishes cleanly. Where and
> >> how can I do this ? in __run__ ? __init__ ? a try/except stuff ?

> > You can have a try/except KeyboardException around the thread code.

>
> > HTH,
> > --
> > Miki

>
> Of course, but I don't know where. I placed this inside loop, within
> called functions from the loop, I still have the problem.
>
> Mathieu


Try this:

loop_completed = True
for i in range(1,50):
try:
# your code here
except KeyboardException:
loop_completed = False
break # this breaks the loop
# end loop
if loop_completed:
# code to be executed in case of normal completion
else:
# code to be executed in case of interruption
# code to be executed in both cases
 
Reply With Quote
 
Mathieu Prevot
Guest
Posts: n/a
 
      08-09-2008
2008/8/8 <(E-Mail Removed)>:
> On 8 Ago, 10:03, "Mathieu Prevot" <(E-Mail Removed)> wrote:
>> 2008/8/8 Miki <(E-Mail Removed)>:
>>
>> > Hello,

>>
>> >> I have a threading.Thread class with a "for i in range(1,50)" loop
>> >> within. When it runs and I do ^C, I have the error [1] as many as
>> >> loops. I would like to catch this exception (and if possible do some
>> >> cleanup like in C pthreads) so the program finishes cleanly. Where and
>> >> how can I do this ? in __run__ ? __init__ ? a try/except stuff ?
>> > You can have a try/except KeyboardException around the thread code.

>>
>> > HTH,
>> > --
>> > Miki

>>
>> Of course, but I don't know where. I placed this inside loop, within
>> called functions from the loop, I still have the problem.
>>
>> Mathieu

>
> Try this:
>
> loop_completed = True
> for i in range(1,50):
> try:
> # your code here
> except KeyboardException:
> loop_completed = False
> break # this breaks the loop
> # end loop
> if loop_completed:
> # code to be executed in case of normal completion
> else:
> # code to be executed in case of interruption
> # code to be executed in both cases


Thanks for answers. My code sheme was the following: main() starts 2
thread trees (threads of threads of ...) and some of these have "for"
loops. These loops needed to be as you recommended:

for ... :
try:
# instructions
except KeyboardInterrupt:
# cleaning instructions
break

The problem with atexit.register is that is doesn't work in case of
system signals catches (http://tinyurl.com/6kdaba)

Thanks,
Mathieu

_____________________________________________
def main():
query1 = Thread1
batch1 = Thread2
while True:
try:
#some code for updating / synchronize / etc threads
except KeyboardInterrupt:
try:
query1.terminate()
batch1.terminate()
except:
pass
finally:
break
_____________________________________________


I used also from http://sebulba.wikispaces.com/recipe+thread2
the following new Thread class:

_____________________________________________
import threading, inspect, ctypes

def _async_raise(tid, exctype):
"""raises the exception, performs cleanup if needed"""
if not inspect.isclass(exctype):
raise TypeError("Only types can be raised (not instances)")
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid,
ctypes.py_object(exctype))
if res == 0:
raise ValueError("invalid thread id")
elif res != 1:
# """if it returns a number greater than one, you're in trouble,
# and you should call it again with exc=NULL to revert the effect"""
ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, 0)
raise SystemError("PyThreadState_SetAsyncExc failed")

class Thread(threading.Thread):
def _get_my_tid(self):
"""determines this (self's) thread id"""
if not self.isAlive():
raise threading.ThreadError("the thread is not active")

# do we have it cached?
if hasattr(self, "_thread_id"):
return self._thread_id

# no, look for it in the _active dict
for tid, tobj in threading._active.items():
if tobj is self:
self._thread_id = tid
return tid

raise AssertionError("could not determine the thread's id")

def raise_exc(self, exctype):
"""raises the given exception type in the context of this thread"""
_async_raise(self._get_my_tid(), exctype)

def terminate(self):
"""raises SystemExit in the context of the given thread, which should
cause the thread to exit silently (unless caught)"""
self.raise_exc(SystemExit)
_____________________________________________
 
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
Thread#raise, Thread#kill, and timeout.rb are unsafe Charles Oliver Nutter Ruby 43 03-25-2008 02:31 PM
Delegate Endinvoke Kill thread chintan jajal via .NET 247 ASP .Net 1 06-06-2005 06:31 PM
KILL BABY KILL widescreen drsd2kill DVD Video 3 11-29-2004 09:36 PM
Bava's KILL BABY KILL widescreen drsd2kill DVD Video 0 11-27-2004 12:04 AM
thread, threading; how to kill a thread? Jerry Sievers Python 12 11-19-2004 12:10 AM



Advertisments