{"id":4581,"date":"2024-01-06T11:57:58","date_gmt":"2024-01-06T11:57:58","guid":{"rendered":"https:\/\/ibex.tech\/python\/?p=4581"},"modified":"2024-01-06T12:01:59","modified_gmt":"2024-01-06T12:01:59","slug":"exiting-a-python-program","status":"publish","type":"post","link":"https:\/\/ibex.tech\/python\/application-control\/exiting-a-python-program","title":{"rendered":"Exiting a python program"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Exit an endless loop Python program<\/h4>\n\n\n\n<p>This method uses an interrupt to capture keyboard CTRL+C entered on the command line, but then cleanly exits from the program&#8217;s main loop.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import signal\nimport time   #Only needed for the usage of sleep()\n\nexit_program_interrupt_triggered = False\n\n#*********************************************\n#*********************************************\n#********** KEYBOARD SIGINT HANDLER **********\n#*********************************************\n#*********************************************\ndef keyboard_signint_signal_handler(signal, frame):\n    global exit_program_interrupt_triggered\n    \n    print(\"Keyboard SIGINT\")\n    exit_program_interrupt_triggered = True\n\nsignal.signal(signal.SIGINT, keyboard_signint_signal_handler)            #signal.SIGINT gives an interrupt when Ctrl+C or Ctrl+F2 is typed on the keyboard\n\n#***********************************\n#***********************************\n#********** MAIN FUNCTION **********\n#***********************************\n#***********************************\n\n    while True:                    #(Do forever)\n\n        #&lt;&lt;&lt;&lt;&lt;Do program stuff\n\n        #----- CHECK FOR EXIT PROGRAM INTERRUPT OCCURED -----\n        if exit_program_interrupt_triggered:\n            print(\"Exiting main loop\")\n            break\n\n        #Sleep to avoid cpu lockup\n        time.sleep(0.01)   #Pause time in seconds\n\n    #END OF while True:\n    print(\"Shutting down\")<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Exit an endless loop Python program This method uses an interrupt to capture keyboard CTRL+C entered on the command line, but then cleanly exits from the program&#8217;s main loop.<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[345],"tags":[],"class_list":["post-4581","post","type-post","status-publish","format-standard","hentry","category-application-control"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4581","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/comments?post=4581"}],"version-history":[{"count":4,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4581\/revisions"}],"predecessor-version":[{"id":4586,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4581\/revisions\/4586"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/media?parent=4581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/categories?post=4581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/tags?post=4581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}