kill thread

bockman at virgilio.it bockman at virgilio.it
Fri Aug 8 06:42:48 EDT 2008


On 8 Ago, 10:03, "Mathieu Prevot" <mathieu.pre... at ens.fr> wrote:
> 2008/8/8 Miki <miki.teb... at gmail.com>:
>
> > 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



More information about the Python-list mailing list