keyboardinterrupt

Why can't I handle a KeyboardInterrupt in python?

杀马特。学长 韩版系。学妹 提交于 2019-11-27 18:20:52
I'm writing python 2.6.6 code on windows that looks like this: try: dostuff() except KeyboardInterrupt: print "Interrupted!" except: print "Some other exception?" finally: print "cleaning up...." print "done." dostuff() is a function that loops forever, reading a line at a time from an input stream and acting on it. I want to be able to stop it and clean up when I hit ctrl-c. What's happening instead is that the code under except KeyboardInterrupt: isn't running at all. The only thing that gets printed is "cleaning up...", and then a traceback is printed that looks like this: Traceback (most

Catching KeyboardInterrupt in Python during program shutdown

断了今生、忘了曾经 提交于 2019-11-27 17:08:00
I'm writing a command line utility in Python which, since it is production code, ought to be able to shut down cleanly without dumping a bunch of stuff (error codes, stack traces, etc.) to the screen. This means I need to catch keyboard interrupts. I've tried using both a try catch block like: if __name__ == '__main__': try: main() except KeyboardInterrupt: print 'Interrupted' sys.exit(0) and catching the signal itself (as in this post ): import signal import sys def sigint_handler(signal, frame): print 'Interrupted' sys.exit(0) signal.signal(signal.SIGINT, sigint_handler) Both methods seem to

what is meant by disabling interrupts?

↘锁芯ラ 提交于 2019-11-27 15:01:25
问题 When entering an inteerupt handler, we first "disable interrupts" on that cpu(using something like the cli instruction on x86). During the time that interrupts are disabled, assume say the user pressed the letter 'a' on the keyboard that would usually cause an interrupt. But since interrupts are disabled, does that mean that: the interrupt handler for 'a' would never be invoked, since interrupts are disabled in the critical section or the interrupt will be handled by the os but delayed, until

Ctrl-C i.e. KeyboardInterrupt to kill threads in Python

≯℡__Kan透↙ 提交于 2019-11-27 13:07:29
问题 I read somewhere that KeyboardInterrupt exception is only raised in the main thread in Python. I also read that the main thread is blocked while the child thread executes. So, does this mean that CTRL + C can never reach to the child thread. I tried the following code: def main(): try: thread = threading.Thread(target=f) thread.start() # thread is totally blocking (e.g., while True) thread.join() except KeyboardInterrupt: print "Ctrl+C pressed..." sys.exit(1) def f(): while True: pass # do

Ctrl-C crashes Python after importing scipy.stats

我与影子孤独终老i 提交于 2019-11-27 07:58:55
I'm running 64-bit Python 2.7.3 on Win7 64-bit. I can reliably crash the Python interpreter by doing this: >>> from scipy import stats >>> import time >>> time.sleep(3) and pressing Control-C during the sleep. A KeyboardInterrupt is not raised; the interpreter crashes. The following is printed: forrtl: error (200): program aborting due to control-C event Image PC Routine Line Source libifcoremd.dll 00000000045031F8 Unknown Unknown Unknown libifcoremd.dll 00000000044FC789 Unknown Unknown Unknown libifcoremd.dll 00000000044E8583 Unknown Unknown Unknown libifcoremd.dll 000000000445725D Unknown

In Matlab, is it possible to terminate a script, but save all its internal variables to workspace?

纵饮孤独 提交于 2019-11-27 07:52:05
I am running a script, but it is taking much too long so I want to terminate the script. However it has calculated a lot of data which I would ideally not want to throw away. Is there an alternative to ctrl-C with which you save the internal function variables to the workspace? Ideally I'm looking for a Matlab keyboard shortcut like ctrl-C , but if that really can't be done maybe there is a way to do this in the script of my function. Any idea how to let my script react to ctrl-C as well, or maybe a GUI element which I can cancel and then I save the variables through my script? Some similar

Why can't I handle a KeyboardInterrupt in python?

坚强是说给别人听的谎言 提交于 2019-11-27 04:16:18
问题 I'm writing python 2.6.6 code on windows that looks like this: try: dostuff() except KeyboardInterrupt: print "Interrupted!" except: print "Some other exception?" finally: print "cleaning up...." print "done." dostuff() is a function that loops forever, reading a line at a time from an input stream and acting on it. I want to be able to stop it and clean up when I hit ctrl-c. What's happening instead is that the code under except KeyboardInterrupt: isn't running at all. The only thing that

What is the difference between Ctrl-C and SIGINT?

为君一笑 提交于 2019-11-27 01:16:35
问题 I have been debugging a Python program which segfaults after receiving a KeyboardInterrupt exception. This is normally done by pressing Ctrl+C from the shell. To test if a particular code change fixed the bug, I had a small shell-script that sent SIGINT to the program at random time after start-up. The problem I have is that sending Ctrl+C seems to have a different effect on the program than sending the signal SIGINT and is thus not causing the bug to appear, so I quite wonder what the

threading ignores KeyboardInterrupt exception

寵の児 提交于 2019-11-26 20:23:37
I'm running this simple code: import threading, time class reqthread(threading.Thread): def run(self): for i in range(0, 10): time.sleep(1) print('.') try: thread = reqthread() thread.start() except (KeyboardInterrupt, SystemExit): print('\n! Received keyboard interrupt, quitting threads.\n') But when I run it, it prints $ python prova.py . . ^C. . . . . . . . Exception KeyboardInterrupt in <module 'threading' from '/usr/lib/python2.6/threading.pyc'> ignored In fact python thread ignore my Ctrl + C keyboard interrupt and doesn't print Received Keyboard Interrupt . Why? What is wrong with this

Cython, Python and KeyboardInterrupt ignored

为君一笑 提交于 2019-11-26 20:14:43
问题 Is there a way to interrupt ( Ctrl+C ) a Python script based on a loop that is embedded in a Cython extension? I have the following python script: def main(): # Intantiate simulator sim = PySimulator() sim.Run() if __name__ == "__main__": # Try to deal with Ctrl+C to abort the running simulation in terminal # (Doesn't work...) try: sys.exit(main()) except (KeyboardInterrupt, SystemExit): print '\n! Received keyboard interrupt, quitting threads.\n' This runs a loop that is part of a C++ Cython