0

I am writing a code where I need to request user input (requested input is used later), but I wanted to always catch the KeyboardInterrupt exception so the script could show a message and request user input (in my case, the Enter key) to stop running the script, so I followed the code suggested by another StackOverflow post.

However, when I try to ask for user input to stop running the script, Python returns the error RuntimeError: can't re-enter readline, so I need to use another approach (wait 5 seconds, then exit) to get a similar functionality, and I'm not sure about what I'm doing wrong (I have searched similar posts related to this error, but I haven't found any applicable to me).

An example code to verify this problem is the following (commented code is the alternative approach I have used).

requestKey = "Press Enter key to exit...\n"
exitTimeout = "\n\nstop received! exit in 5 seconds...\n"

import sys, signal, threading, time

# https://stackoverflow.com/questions/4205317/capture-keyboardinterrupt-in-python-without-try-except
def exitKey(signal, frame):
    #print(exitTimeout)
    #time.sleep(5)
    input(requestKey)
    sys.exit()

signal.signal(signal.SIGINT, exitKey)
watchExit = threading.Event()

while True:
    userInput = input("\nenter something: ")
    print("user input: " + userInput)

EDIT: an example of the error returned when running the code is the below.

enter something: ^CTraceback (most recent call last):
  File "/Users/user/Documents/signalError.py", line 17, in <module>
    userInput = input("\nenter something: ")
  File "/Users/user/Documents/signalError.py", line 10, in exitKey
    input(requestKey)
RuntimeError: can't re-enter readline
noeXzTi
  • 1
  • 2
  • Is this above the `signalError.py`? Are you using Linux of Windows. I use Windows and I have no errors – Muddyblack k Apr 22 '23 at 20:24
  • And what do you try to achieve? If you just want to exit you could make an if request if the `userInput == ""` and then `sleep` and `exit` – Muddyblack k Apr 22 '23 at 20:26
  • If the code doesn't use multithreading or multiprocessing, simply catching `KeyboardInterrupt` with try-except may be a better solution than a signal handler. – Michael Butscher Apr 22 '23 at 20:36
  • @Muddyblackk I am using macOS 13, but this error also happens on a Windows 11 x64 installation. The code is indeed the file `signalError.py`. If I use a `if` request, then it would only catch the `KeyboardInterrupt` exception when the script is requesting user input, ignoring it when running the following code (the code I have posted is just an example, not the actual code [which is irrelevant to the question]). You might not be seeing the error because you are not triggering the `KeyboardInterrupt` exception by pressing Ctrl +C. – noeXzTi Apr 23 '23 at 10:22
  • @MichaelButscher I didn't want to use a `try` - `except` in my code, which is why I followed the suggestion of the StackOverflow post that I have linked. However, if there's no way to fix this, I may stick to the 5 seconds timeout I'm currently using. – noeXzTi Apr 23 '23 at 10:25

0 Answers0