Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
How to kill a script?
-
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 <CTL>C. Of course, that's not an option on an iPad. What's the routine? Thanks.
-
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:)
-
@Involute, just to be sure: you are not looking at the X at the top of the screen?
-
@KnightExcalibur Thanks, but I'm not using scene.
-
@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.
-
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.
-
@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.
-
@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')
-
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.
-
@JonB except Exception only to get its value the x button allows to leave the loop
-
@cvp That seems to be working. Thanks.