1

I'm trying to do something in a loop (in this example print to 100) and cancel the count at any time by pressing 'Ctrl+C'.

The test code I'm using is below, however this doesn't work (EDIT: it works in a script launched from terminal but does not work when run in the python shell of my IDE - Wing 101). KeyboardInterrupt doesn't catch the 'Ctrl+C' command or any other key.

import time     

i = 1
while (i < 101):
    try:
        print '%d percent complete' % i
        time.sleep(0.5)
        i += 1
    except KeyboardInterrupt:
        break 

I've read this question here and I'm pretty sure that's not my problem because I've put the 'time.sleep(0.5)' command in to slow the program down. I've also read this question but since it's 5 years old now I assume this bug would have been fixed by now? I know that I can use threading to achieve what I want, but if only for the sake of learning I'd like to know why this method doesn't work.

I'm using python 2.7.6 in Ubuntu 14.04 and I'd appreciate any help or advice in solving this issue. (EDIT: I know the code works in isolation, but I'd still be interested in knowing why it doesn't work in the python shell of Wing 101 IDE)

EDIT 1

I've tried putting the while loop inside the try block, as suggested:

try:
    i = 1
    while (i < 101):        
        print '%d percent complete' % i
        time.sleep(0.5)
        i += 1
except KeyboardInterrupt:
    break 

But unfortunately that doesn't work either, instead I get the following error:

Traceback (most recent call last):
  File "/home/matt/autosys_repo1/python_test_scripts/test_refresh_line.py", line 13, in <module>
    break
'break' outside loop: <string>, line 13
Community
  • 1
  • 1
Matt
  • 57
  • 8
  • There's a difference between your approach and an answer in the first thread you've linked. The guy suggests putting the loop sentences inside the try block. You have the while sentence outside the try/except. Have you tried doing it that way? Edit: I've tried your code and it works as expected. – pazitos10 Jul 20 '16 at 01:21
  • Thanks for the suggestion @Brnpzs. Unfortunately that doesn't work either (see edit 1). And yes, it seems to work for everyone else so I'm confused as to why it won't work for me. I feel like its just some small detail I'm overlooking. – Matt Jul 20 '16 at 02:00
  • break has no sense outside while loop, you can use sys.exit(0) to force the exit. To do so, you need to import sys module first. – pazitos10 Jul 20 '16 at 02:04
  • Doesn't work either I'm afraid. It just continues printing and never exits the loop until i = 100. I think the key problem is KeyboardInterrupt not recognising the key press. – Matt Jul 20 '16 at 02:08
  • Another way to solve this problem is using [signal handlers](https://docs.python.org/3/library/signal.html#example) , in this case, you want to handle SIGINT as shown in the answer of [this](http://stackoverflow.com/questions/6990474/how-can-i-override-the-keyboard-interrupt-python) question. – pazitos10 Jul 20 '16 at 02:15
  • Ah, hang on. I've just realised that the original code snippet _does_ work, but only if I run it in a script by itself. If I run it in the python shell in my IDE (Wing 101) it doesn't work, nor does it work as part of my larger program (but that could be for several other underlying reasons). I will update the title and description accordingly. – Matt Jul 20 '16 at 02:45
  • I think the problem is related to the IDE, in [this](http://tutor.python.narkive.com/ghn4XCmd/keyboard-interrupt) "thread", someone says that you cannot simply do ctrl+c while using wing IDE but you can do option > restart shell to do the trick. – pazitos10 Jul 20 '16 at 04:00
  • That's unfortunate but at least I've learned something! Thanks for your help. – Matt Jul 20 '16 at 22:15

1 Answers1

0

This is a limitation of Wing IDE 101. The only way to stop a loop like this is to restart the shell.