Questions tagged [keyboardinterrupt]

227 questions
141
votes
10 answers

Keyboard Interrupts with python's multiprocessing Pool

How can I handle KeyboardInterrupt events with python's multiprocessing Pools? Here is a simple example: from multiprocessing import Pool from time import sleep from sys import exit def slowly_square(i): sleep(1) return i*i def go(): …
Fragsworth
  • 28,413
  • 24
  • 76
  • 96
108
votes
7 answers

Capture keyboardinterrupt in Python without try-except

Is there some way in Python to capture KeyboardInterrupt event without putting all the code inside a try-except statement? I want to cleanly exit without trace if user presses Ctrl+C.
Alex
  • 1,804
  • 4
  • 22
  • 27
93
votes
2 answers

Catching KeyboardInterrupt in Python during program shutdown

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…
Dan
  • 2,112
  • 3
  • 17
  • 25
60
votes
5 answers

threading ignores KeyboardInterrupt exception

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…
Emilio
  • 3,711
  • 9
  • 40
  • 49
43
votes
5 answers

Python: How to prevent subprocesses from receiving CTRL-C / Control-C / SIGINT

I am currently working on a wrapper for a dedicated server running in the shell. The wrapper spawns the server process via subprocess and observes and reacts to its output. The dedicated server must be explicitly given a command to shut down…
robert
  • 3,017
  • 3
  • 25
  • 35
42
votes
6 answers

Why can't I handle a KeyboardInterrupt in python?

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…
Josh
  • 1,889
  • 2
  • 17
  • 22
37
votes
7 answers

Ctrl-C crashes Python after importing scipy.stats

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;…
BrenBarn
  • 210,788
  • 30
  • 364
  • 352
36
votes
2 answers

What is the difference between Ctrl-C and SIGINT?

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…
Belorn
  • 371
  • 1
  • 3
  • 5
30
votes
3 answers

Why does the asyncio's event loop suppress the KeyboardInterrupt on Windows?

I have this really small test program which does nothing apart from a executing an asyncio event loop: import asyncio asyncio.get_event_loop().run_forever() When I run this program on Linux and press Ctrl+C, the program will terminate correctly…
24
votes
7 answers

Remove traceback in Python on Ctrl-C

Is there a way to keep tracebacks from coming up when you hit Ctrl+c, i.e. raise KeyboardInterrupt in a Python script?
Kyle Hotchkiss
  • 9,348
  • 18
  • 51
  • 81
24
votes
2 answers

Catching `KeyboardInterrupt` without closing Selenium Webdriver sessions in Python

A Python program drives Firefox via Selenium WebDriver. The code is embedded in a try/except block like this: session = selenium.webdriver.Firefox(firefox_profile) try: # do stuff except (Exception, KeyboardInterrupt) as exception: …
Eleno
  • 2,534
  • 2
  • 28
  • 37
22
votes
3 answers

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

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…
Amit S
  • 1,033
  • 2
  • 9
  • 18
18
votes
3 answers

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

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…
Leo
  • 1,667
  • 3
  • 19
  • 42
14
votes
4 answers

Cython, Python and KeyboardInterrupt ignored

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__ ==…
Gauthier Boaglio
  • 9,308
  • 4
  • 43
  • 81
13
votes
5 answers

what is meant by disabling interrupts?

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…
trohit
  • 133
  • 1
  • 1
  • 5
1
2 3
15 16