Python Forum
How to exit infinite While Loop?
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to exit infinite While Loop?
#1
On a Windows PC I'm working on some code to do a task every 'n' minutes.

Normally the program will run constantly unless the system user/admin wishes to stop it.

The plans were to use a while loop to do the job and sleep to create the 'n' minute delay between each time the task gets done.

But, once the system goes into an 'infinite' while loop... what is the proper way for the user/admin to end the program (while loop) whenever they so decide?

Thanks for any help.
Reply
#2
You could try a 'KeyboardInterrupt' exception, say "Control + C to exit"
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
You could also just have it happen one time, and not have a while loop at all, then use the OS' scheduler to run it every N minutes.
Reply
#4
You could use another script to open a port. So the first script will check it and if it's open will stop the execution of the loop. You don't even need to set a value to check for or some kind of RPC
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Shocked Why this code does not go into an infinite loop? 2367409125 2 836 Dec-02-2022, 08:22 PM
Last Post: deanhystad
  python difference between sys.exit and exit() mg24 1 1,764 Nov-12-2022, 01:37 PM
Last Post: deanhystad
  Need help with infinite loop & making hotkeys/shortcuts Graxum 1 1,124 Aug-22-2022, 02:57 AM
Last Post: deanhystad
  While loop doesn't exit KenHorse 3 1,967 Jun-20-2021, 11:05 PM
Last Post: deanhystad
  Infinite loop problem Zirconyl 5 2,923 Nov-16-2020, 09:06 AM
Last Post: DeaD_EyE
  using 'while loop' output going into infinite loop... amitkb 2 1,910 Oct-05-2020, 09:18 PM
Last Post: micseydel
  Exit Function - loop Tetsuo30 2 2,025 Sep-17-2020, 09:58 AM
Last Post: Tetsuo30
  Infinite loop not working pmp2 2 1,609 Aug-18-2020, 12:27 PM
Last Post: deanhystad
  Struggling to exit this while loop fatherted99 5 2,413 Feb-08-2020, 07:46 PM
Last Post: fatherted99
  Why is this code not an infinite loop? psi2kgcm 1 1,802 Dec-24-2019, 02:04 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020