-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can I catch a SIGINT (Ctrl+C) and stop a function called by a FunctionItem? #55
Comments
This has nothing to do with this module, this is a python question. I recommend you look up into "Exceptions" and "Error Handling", that's what you are looking for. Every time you press CTRL+C python throws a KeyboardInterrupt exception and your program can catch it and treat it itself (instead of leaving for the normal handling python does). Use my following code inside your "long running functions" and change the "sys.exit()" for your own thing. import sys, time
var = 1
try:
while(True):
var += 0.1
time.sleep(0.1)
except KeyboardInterrupt:
print("var = ", var)
sys.exit() Here are super useful links:
Have fun! |
Actually, I've tried to do the handling the way I've described to you and it didn't work.... I don't know why, but I suspect the menu application is catching the exception itself. I stopped investigating because I figured out "Ctrl + D" works fine to kill the current thread and get me back to my Main Menu. Maybe it could work for you if you don't have submenus nested as in my application. |
Hi @FelipeEmos I am looking for an answer to the same issue (capturing Ctrl-C inside a FunctionItem function). |
I think this is related to this, so adding it here, rather than starting a new Issue: Simplified code to show the issue I ran into: This will raise a KeyError and cause an exit, as expected:
This will sometimes flash the Exception text in the terminal, but ultimately returns to the menu:
To rule out the while loop somehow causing the issue:
What led me to this was not being able to catch the exceptions raised from menu.show()...: This makes it seem like menu.show() is dumping the Exception to the terminal and forcing an exit instead of raising it up to the calling function...:
|
Title sums it up. I use a menu to start long running functions and would like to stop them catching a CTRL+C. How can I do this?
The text was updated successfully, but these errors were encountered: