0

I have a program that takes a very long time to run, and I want it to be able to do what it needs to do but then if the user hits a specific key then I want the loop to break at a certain point. Most Q&A's I have seen pertaining to this problem prompt the user to enter something on each iteration of the loop, however I do not want this. I want the loop to run undisturbed until it needs to quit.

The script will be running as an executable in a command prompt (Windows) so I guess the user could just close the window but I want the loop to break at a certain point. For example:

while True:
 print "Doing whatever I feel like"
 userinput = raw_input()
 if userinput == 'q':
    break

So this keeps printing until the user enters 'q', but it prompts the user for input each iteration. How can I avoid this?

4 Answers 4

2

If you do not need to stop at a specific point, but just to be able to stop it, you could use a try/except with KeyboardInterrupt (Ctrl-C).

try:
    while True:
        print "Doing whatever I feel like"
except KeyboardInterrupt:
    exit

When the user hits CTRL-C it will exit.

Sign up to request clarification or add additional context in comments.

1 Comment

I actually like this. I'll have to add another try-except block in my code but this seems pretty reasonable
1

Or this:

import msvcrt        
while True:
    print "Doing whatever I feel like"
    if msvcrt.kbhit():              # True if a keypress is waiting to be read.
        if msvcrt.getch()=="q":     # will not wait for Enter to be pressed
            break

Check msvcrt.

2 Comments

This looks pretty promising (aside from the fact that it only works on windows). Is it possible to run this in a python terminal / ipython console or only when run in an executable script? I tried in ipython console and no response to keyboard entry.
Not sure. But if you want to use it in a terminal, I would try putting this code in a function, and then call the function. Not sure whether it will make a difference, but it might.
1

Two possible solutions:
1. Press Ctrl-C to close your program. This also works on linux.
2.

while True:
  for _ in range(100)
    print "Doing whatever I feel like"
  userinput = raw_input()
  if userinput == 'q':
    break

This only asks the user every 100 iterations.

1 Comment

I'll be running this program over night basically so I need it to complete fully however Ctlr-C would be ok with a try statement
1

Start a separate Thread to perform the computation that you need to perform while self.flag == False, and the main program can just sit there waiting for the user input. Once the user input is given, set Thread.flag = True, which will stop the Thread. Wait for the Thread to finish and join, then you can exit from the main program as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.