2008/8/8 <bo*****@virgil io.it>:
On 8 Ago, 10:03, "Mathieu Prevot" <mathieu.pre... @ens.frwrote:
>2008/8/8 Miki <miki.teb...@gm ail.com>:
Hello,
>I have a threading.Threa d 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 KeyboardExcepti on 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 KeyboardExcepti on:
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 KeyboardInterru pt:
# 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 KeyboardInterru pt:
try:
query1.terminat e()
batch1.terminat e()
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(ti d, exctype):
"""raises the exception, performs cleanup if needed"""
if not inspect.isclass (exctype):
raise TypeError("Only types can be raised (not instances)")
res = ctypes.pythonap i.PyThreadState _SetAsyncExc(ti d,
ctypes.py_objec t(exctype))
if res == 0:
raise ValueError("inv alid 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.pythonap i.PyThreadState _SetAsyncExc(ti d, 0)
raise SystemError("Py ThreadState_Set AsyncExc failed")
class Thread(threadin g.Thread):
def _get_my_tid(sel f):
"""determin es this (self's) thread id"""
if not self.isAlive():
raise threading.Threa dError("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._acti ve.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(se lf._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)
_______________ _______________ _______________