Can I Make Use Of An Interrupt To Print A Status While Still Continue Process?
In python, is it possible to make use of KeyboardInterrupt or CTRL+C to print a status message, possibly like printing content of a variable and then continuing with the execution?
Solution 1:
You can do this using a signal handler:
import signal
defsigint_handler(signum, frame):
print"my_variable =", frame.f_locals.get("my_variable", None)
signal.signal(signal.SIGINT, sigint_handler)
Now interrupting the script calls the handler, which prints the variable, fishing it out of the current stack frame. The script then continues.
Solution 2:
It can be done. The signal
library provides this functionality, and it pretty much goes the way you prototyped.
import signal
interrupted = Falsedefsignal_handler(signum, frame):
global interrupted
interrupted = True
signal.signal(signal.SIGINT, signal_handler)
while true:
update(V)
if interrupted:
print V
Solution 3:
Pressing ctrl+c raises KeyboardInterrupt.
Catch KeyboardInterrupt and print a message from it, or use a state variable like you have above.
Post a Comment for "Can I Make Use Of An Interrupt To Print A Status While Still Continue Process?"