omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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?

    Pythonista
    5
    11
    4254
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Involute
      Involute last edited by

      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.

      mikael 1 Reply Last reply Reply Quote 0
      • KnightExcalibur
        KnightExcalibur last edited by 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:)

        Involute 1 Reply Last reply Reply Quote 0
        • mikael
          mikael @Involute last edited by

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

          Involute 1 Reply Last reply Reply Quote 0
          • Involute
            Involute @KnightExcalibur last edited by

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

            1 Reply Last reply Reply Quote 0
            • Involute
              Involute @mikael last edited by

              @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.

              1 Reply Last reply Reply Quote 0
              • JonB
                JonB last edited by

                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.

                1 Reply Last reply Reply Quote 0
                • Involute
                  Involute last edited by

                  @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 1 Reply Last reply Reply Quote 0
                  • cvp
                    cvp @Involute last edited by

                    @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') 
                    
                    Involute 1 Reply Last reply Reply Quote 0
                    • JonB
                      JonB last edited by

                      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 1 Reply Last reply Reply Quote 0
                      • cvp
                        cvp @JonB last edited by

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

                        1 Reply Last reply Reply Quote 0
                        • Involute
                          Involute @cvp last edited by

                          @cvp That seems to be working. Thanks.

                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post
                          Powered by NodeBB Forums | Contributors