Forum Archive

How to kill a script?

Involute

I have a script running in an infinite loop and I can't stop it short of closing Pythonista. On a PC I'd hit C. Of course, that's not an option on an iPad. What's the routine? Thanks.

KnightExcalibur

I asked a similar question a few days ago. If you are using scene, I quote pulbrich in his response:

  • pulbrich posted 4 days ago

In the Game class you can include a

def stop(self):
...
method. It gets called automatically when a scene is stopped (by tapping the “x” button).

If this is a script that is running in the console, I imagine it would require a different approach. I'll leave that to the experienced guys:)

mikael

@Involute, just to be sure: you are not looking at the X at the top of the screen?

Involute

@KnightExcalibur Thanks, but I'm not using scene.

Involute

@mikael I can see the X at the top right, but when I tap on it nothing happens. Should have said that originally. I should add that I have the same problem in IDLE on my PC running the same script, so I assume it's a Python issue and not a Pythonista issue.

JonB

Don't create infinite loops.

The X in the console basically issues a KeyboardInterrupt. But if your loop uses try/except all, you won't be able to cancel it. Be sure to always only catch the exceptions specific to your code.

Involute

@JonB said:

Don't create infinite loops.

Well, I need to repeatedly request input from the user. How else can I do that except something like while(True)? I also use try/except since the input should only be a number and the user may enter a letter or something else by mistake. The user can enter 4 to exit, and that function issues a sys.exit(), but that has no effect.

cvp

@Involute this script can be stopped by the x at top right

while True:
    try:
        t = int(input())
        print(t)
    except KeyboardInterrupt:
        break
    except Exception as e:
        #print(e)
        print('input is not integer') 
JonB

except Exception is also a little questionable -- you are probably getting a specific exception , catch that one.

while True is probably okay, just make sure there is a parh to exit the loop.

cvp

@JonB except Exception only to get its value the x button allows to leave the loop

Involute

@cvp That seems to be working. Thanks.