3

Usually I can interrupt stuff with Ctrl+C, but sometimes when I'm using threads it doesn't work - example below.

Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> time.sleep(100)
^CTraceback (most recent call last):
  File "<stdin>", line 1, in <module>
K    eyboardInterrupt
>>> import Queue
>>> q = Queue.Queue(maxsize=3)
>>> q.put(0)
>>> q.put(1)
>>> q.put(2)
>>> q.put(3)
^C^C^C^C^C^C^C^C

^C^C^C

^C^C
^C
@*#()#@#@$!!!!!

edit: Is there a way to get back to the interpreter? Solutions so far kill python completely and your existing namespace ..

wim
  • 338,267
  • 99
  • 616
  • 750

3 Answers3

3

You can kill the Python interpreter with Ctrl+\.

This will send a SIGQUIT instead of SIGINT.

phihag
  • 278,196
  • 72
  • 453
  • 469
  • Not if he has a stock Ubuntu system. – ʇsәɹoɈ Sep 06 '11 at 07:23
  • 1
    @ʇsәɹoɈ Oops, you're right, I accidentally included a totally wrong shortcut (that explains the rofl from Matt). Don't know where my mind was. Back *slash* terminates python, alt+Back *space* terminates X. – phihag Sep 06 '11 at 07:29
  • thanks .. will the same thing happen behind the scenes if i just shut the terminal window? (oh look, a canberran!) – wim Sep 06 '11 at 09:11
  • @wim No, that will send a `SIGTERM` and/or `SIGKILL`. In practice, unless you messed with [signal interception](http://docs.python.org/library/signal.html), the effect will be the same though. – phihag Sep 06 '11 at 09:17
1

A quick workaround for ^C failing is suspending the process with all threads first with ^Z and then killing it.

This works in Linux for many cases when ^C fails, and as I've just tested it works here too (tested on Python v.2.6.5):

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) [GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Queue
>>> q = Queue.Queue(maxsize=3)
>>> q.put(0)
>>> q.put(1)
>>> q.put(2)
>>> [^C]
KeyboardInterrupt #does not kill the process
>>> [^Z - Suspends and exits to shell]
[1]+  Stopped                 python
#mdf:~$ kill -9 %%
[1]+  Killed                  python
flouris
  • 106
  • 2
0

A lazy way to do this is to open another window.

Do ps to get the PID.

Do kill to kill the offending process.

S.Lott
  • 384,516
  • 81
  • 508
  • 779