Questions tagged [keyboardinterrupt]

280 questions
165
votes
11 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
  • 33,919
  • 27
  • 84
  • 97
138
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,952
  • 4
  • 23
  • 29
121
votes
6 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
  • 2,009
  • 6
  • 24
  • 27
67
votes
6 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,901
  • 11
  • 44
  • 50
51
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
  • 521
  • 1
  • 4
  • 5
45
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,484
  • 3
  • 29
  • 38
44
votes
7 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
  • 2,039
  • 3
  • 20
  • 25
44
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
  • 242,874
  • 37
  • 412
  • 384
33
votes
9 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
  • 10,754
  • 20
  • 56
  • 82
33
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…
skrause
  • 989
  • 1
  • 9
  • 14
25
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,864
  • 3
  • 33
  • 39
22
votes
3 answers

How to kill a child thread with Ctrl+C?

I would like to stop the execution of a process with Ctrl+C in Python. But I have read somewhere that KeyboardInterrupt exceptions are only raised in the main thread. I have also read that the main thread is blocked while the child thread executes.…
Amit S
  • 1,053
  • 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,757
  • 3
  • 20
  • 44
17
votes
9 answers

Why doesn't this Python keyboard interrupt work? (in PyCharm)

My Python try/except loop does not seem to trigger a keyboard interrupt when Ctrl + C is pressed while debugging my code in PyCharm. (The same issue occurs when using Ctrl + C while running the program, but not in the PyCharm Python console.) My…
Edwin Shepherd
  • 413
  • 1
  • 8
  • 17
16
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
  • 163
  • 1
  • 1
  • 5
1
2 3
18 19