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.


    Correctly exiting an app

    Pythonista
    4
    4
    3143
    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.
    • drscheme
      drscheme last edited by

      I have an app that runs in a while true loop. I want to exit the app correctly. How can I do this?

      On a typical console app I'd use a "try ... except keyboardinterrupt" construct but this doesn't work in my app.

      Any suggestions?

      Regards

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

        I am a beginner, but I will try to help

        Not sure what you are looking for. To answer correctly I think you need to give more context of your app. But the below code prints something then exits first time through the loop. Not sure it helps or not. If not, I think better to rewrite your question and include some code.

        import contacts
        import sys
        
        my_contacts = contacts.get_all_people()
        for person in my_contacts:
        	print dir(person)
        	sys.exit(0)				
        
        
        1 Reply Last reply Reply Quote 0
        • JonB
          JonB last edited by

          one option is to use ui, and show a button, which sets a global upon key press, which your while true loop checks for.

          you can press the x button in the ui, which throws a keyboardinterrupt, but pythonista will not let you catch that exception.

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

            The most readable way to end an infinite loop is to break out of the loop when some condition is met.

            # top_of_the_minute: loop forever until seconds is zero
            import datetime
            print('Waiting {}...'.format(datetime.datetime.now().second))
            while True:  # break out of the infinite loop when seconds is zero
                if not datetime.datetime.now().second:
                    break  # or exit('Done.') or quit('Done.') or raise SystemExit('Done.') or sys.exit('Done.')
            print('Done.')
            

            I often use sys.exit() as mentioned by @Phukett2 above but raise SystemExit or exit([text or number]) or quit([text or number]) work just as well and do not require an import.

            See http://stackoverflow.com/questions/19747371/python-exit-commands-why-so-many-and-when-should-each-be-used

            break is best but raise SystemExit('Done.') works well too.

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