FAQ
Hello.

I am running Fedora Linux and KDE, using the Konsole command line.

When coding Python, I regularly make a bug causing my program to not
terminate. But how do I kill the non-terminating Python interpreter
without killing the entire Konsole?

The default way of killing the current process on the command line is
Ctrl+C, but that doesn't work with Python. Neither do the "terminate
task", "suspend task" or "interrupt task" commands (available from
right-click in Konsole).

So, can someone please help? How do I kill Python without having to
restart Konsole?

Thanks in advance.

Search Discussions

  • Diez B. Roggisch at May 8, 2008 at 3:50 pm

    spectrumdt at gmail.com wrote:

    Hello.

    I am running Fedora Linux and KDE, using the Konsole command line.

    When coding Python, I regularly make a bug causing my program to not
    terminate. But how do I kill the non-terminating Python interpreter
    without killing the entire Konsole?

    The default way of killing the current process on the command line is
    Ctrl+C, but that doesn't work with Python. Neither do the "terminate
    task", "suspend task" or "interrupt task" commands (available from
    right-click in Konsole).

    So, can someone please help? How do I kill Python without having to
    restart Konsole?

    put it into the background using C-z, and then look for it's pid using ps.
    Issue a kill -9 towards it.

    For example, I just in the moment use this commandline to do so (for a
    certain script named frontend_cherrypy.py:

    ps ux | grep frontend | kill -9 `cut -c 5-15`

    Diez
  • Jean-Paul Calderone at May 8, 2008 at 3:50 pm

    On Thu, 8 May 2008 08:29:33 -0700 (PDT), spectrumdt at gmail.com wrote:
    Hello.

    I am running Fedora Linux and KDE, using the Konsole command line.

    When coding Python, I regularly make a bug causing my program to not
    terminate. But how do I kill the non-terminating Python interpreter
    without killing the entire Konsole?

    The default way of killing the current process on the command line is
    Ctrl+C, but that doesn't work with Python. Neither do the "terminate
    task", "suspend task" or "interrupt task" commands (available from
    right-click in Konsole).
    Ctrl+C often works with Python, but as with any language, it's possible
    to write a program which will not respond to it. You can use Ctrl+\
    instead (Ctrl+C sends SIGINT which can be masked or otherwise ignored,
    Ctrl+\ sends SIGQUIT which typically isn't) or you can use the kill
    command. You can either use it in another terminal or you can suspend
    the Python process (Ctrl+Z which sends SIGSTOP and _cannot_ be masked
    or otherwise ignored) and then use kill in that terminal. You need to
    know the PID or jobspec in order to use kill. In bash (and probably
    elsewhere), jobspecs are easy. When you hit Ctrl+Z, the jobspec will
    be written to your terminal in the form "[N]+ Stopped python". "N"
    is the jobspec and you can kill it with "kill %N". You may need to use
    SIGKILL to end the process, in which case use "kill -KILL %N". You can
    then resume the Python process with the "fg" command, it will immediately
    end since it has be killed.

    This is more a Linux/bash/etc question than a Python question, so followups
    might be directed to a more appropriate forum (eg, the man page for your
    shell, or a bash user group, etc).

    Jean-Paul
  • Marco Mariani at May 8, 2008 at 3:51 pm

    spectrumdt at gmail.com wrote:

    The default way of killing the current process on the command line is
    Ctrl+C, but that doesn't work with Python.
    It should work.

    Do you have a bare except: which intercepts SystemExit as well?

    If so, be as specific as possible in what you intercept, or at least
    catch StandardError:

    In [1]: StandardError ?
    Type: type
    Base Class: <type 'type'>
    String Form: <type 'exceptions.StandardError'>
    Namespace: Python builtin
    Docstring:
    Base class for all standard Python exceptions that do not represent
    interpreter exiting.
  • Notbob at May 8, 2008 at 5:30 pm

    On 2008-05-08, spectrumdt at gmail.com wrote:

    I am running Fedora Linux and KDE, using the Konsole command line.
    I also run python from Konsole.
    When coding Python, I regularly make a bug causing my program to not
    terminate. But how do I kill the non-terminating Python interpreter
    without killing the entire Konsole?
    Are you refering to the python editor? If so, try cntrl-d.
    The default way of killing the current process on the command line is
    Ctrl+C, but that doesn't work with Python. Neither do the "terminate
    task", "suspend task" or "interrupt task" commands (available from
    right-click in Konsole).
    If you want to completely kill python, open another Konsole session and kill
    it from there. There are several ways. The simplist is:

    killall python

    ....which will kill python without killing the Konsole session.

    This will find the python pid number and kill it with -15 which cleans
    everything up nicely before killing. If that doesn't work, you may need to
    use kill -9, which is kill with extreme prejudice and leaves all the bodies
    lying around to crap up the works. To do that, try:

    ps aux | grep python

    .....which will give you the pid number and then you plug it into:

    kill -9 pid_number

    If all that doesn't work, change to Slackware! ;)

    nb
  • Spectrumdt at May 9, 2008 at 10:16 am
    Thanks for all the replies.
    On May 8, 5:50?pm, Jean-Paul Calderone wrote:
    Ctrl+C often works with Python, but as with any language, it's possible
    to write a program which will not respond to it. ?You can use Ctrl+\
    instead (Ctrl+C sends SIGINT which can be masked or otherwise ignored,
    Ctrl+\ sends SIGQUIT which typically isn't)
    Yes, thank you, this seems to work. :)

    I did some more testing and found out that the problem seems to be
    thread-related. If I have a single-threaded program, then Ctrl+C
    usually works, but if I have threads, it is usually ignored. For
    instance, the below program does not respond to Ctrl+C (but it does
    die when issued Ctrl+\):



    import threading

    def loop():
    while True:
    pass

    threading.Thread(target=loop,args=()).start()
  • Dmitrey at May 9, 2008 at 10:22 am
    in addition to killall and kill funcs mentioned above you could be
    interested in pkill
    see "man pkill" from terminal for more details

    pkill python

    or

    pkill pyt

    or

    pkill <desired_signal> py
  • Spectrumdt at May 9, 2008 at 10:19 am
    Thanks for the replies.
    On May 8, 5:50?pm, Jean-Paul Calderone wrote:
    Ctrl+C often works with Python, but as with any language, it's possible
    to write a program which will not respond to it. ?You can use Ctrl+\
    instead (Ctrl+C sends SIGINT which can be masked or otherwise ignored,
    Ctrl+\ sends SIGQUIT which typically isn't)
    Yes, thank you, this seems to work. :)

    I did some more testing and found out that the problem seems to be
    thread-related. If I have a single-threaded program, then Ctrl+C
    usually works, but if I have threads, it is usually ignored. For
    instance, the below program does not respond to Ctrl+C (but it does
    die when issued Ctrl+\):



    import threading

    def loop():
    while True:
    pass

    threading.Thread(target=loop,args=()).start()
  • Floris Bruynooghe at May 9, 2008 at 11:41 am

    On May 9, 11:19?am, spectru... at gmail.com wrote:
    Thanks for the replies.
    On May 8, 5:50?pm, Jean-Paul Calderone wrote:

    Ctrl+C often works with Python, but as with any language, it's possible
    to write a program which will not respond to it. ?You can use Ctrl+\
    instead (Ctrl+C sends SIGINT which can be masked or otherwise ignored,
    Ctrl+\ sends SIGQUIT which typically isn't)
    Yes, thank you, this seems to work. :)

    I did some more testing and found out that the problem seems to be
    thread-related. If I have a single-threaded program, then Ctrl+C
    usually works, but if I have threads, it is usually ignored. For
    instance, the below program does not respond to Ctrl+C (but it does
    die when issued Ctrl+\):

    import threading

    def loop():
    ? while True:
    ? ? pass

    threading.Thread(target=loop,args=()).start()
    Your thread needs to be daemonised for this work. Othewise your main
    thread will be waiting for your created thread to finish. E.g.:

    thread = threading.Thread(target=loop)
    thread.setDaemon(True)
    thread.start()

    But now it will exit immediately as soon as your main thread has
    nothing to do anymore (which is right after .start() in this case), so
    plug in another infinite loop at the end of this:

    while True:
    time.sleep(10)

    And now your threaded app will stop when using C-c.
  • Banibrata Dutta at May 9, 2008 at 1:17 pm
    Any chance that there is a "catch-all" in the code.
    As per PEP8 and I'm sure common knowledge of all the folks programming in
    Python for a while -- a
    catch:
    could just lead to this problem.

    On 5/9/08, Floris Bruynooghe wrote:
    On May 9, 11:19 am, spectru... at gmail.com wrote:
    Thanks for the replies.
    On May 8, 5:50 pm, Jean-Paul Calderone wrote:

    Ctrl+C often works with Python, but as with any language, it's possible
    to write a program which will not respond to it. You can use Ctrl+\
    instead (Ctrl+C sends SIGINT which can be masked or otherwise ignored,
    Ctrl+\ sends SIGQUIT which typically isn't)
    Yes, thank you, this seems to work. :)

    I did some more testing and found out that the problem seems to be
    thread-related. If I have a single-threaded program, then Ctrl+C
    usually works, but if I have threads, it is usually ignored. For
    instance, the below program does not respond to Ctrl+C (but it does
    die when issued Ctrl+\):

    import threading

    def loop():
    while True:
    pass

    threading.Thread(target=loop,args=()).start()
    Your thread needs to be daemonised for this work. Othewise your main
    thread will be waiting for your created thread to finish. E.g.:

    thread = threading.Thread(target=loop)
    thread.setDaemon(True)
    thread.start()

    But now it will exit immediately as soon as your main thread has
    nothing to do anymore (which is right after .start() in this case), so
    plug in another infinite loop at the end of this:

    while True:
    time.sleep(10)

    And now your threaded app will stop when using C-c.
    --
    http://mail.python.org/mailman/listinfo/python-list


    --
    regards,
    Banibrata
    http://www.linkedin.com/in/bdutta
    http://octapod.wordpress.com
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20080509/bcd03428/attachment.htm>
  • Thomas Bellman at May 10, 2008 at 9:45 am

    Jean-Paul Calderone wrote:

    (Ctrl+Z which sends SIGSTOP and _cannot_ be masked
    or otherwise ignored)
    Bzzt! Ctrl-Z causes a SIGTSTP to be sent, not SIGSTOP, and
    SIGTSTP can be both caught, ignored and masked.


    --
    Thomas Bellman, Lysator Computer Club, Link??ping University, Sweden
    "You cannot achieve the impossible without ! bellman @ lysator.liu.se
    attempting the absurd." ! Make Love -- Nicht Wahr!

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMay 8, '08 at 3:29p
activeMay 10, '08 at 9:45a
posts11
users9
websitepython.org

People

Translate

site design / logo © 2022 Grokbase