-4

None of these are kill -9 or anything close. What's the answer?

import sys 
import os

try:
        exit()
        sys.exit()
        os._exit()
        quit()
except:
        print("THIS SHOULD NOT RUN")

It's ridiculous of the Python language if any try-catch wrapping of an abort() or anything like it prevent's it from being able to die.

That's not something that exists in any other language that I know. abort() is an emergency safety measure.

Sayse
  • 42,633
  • 14
  • 77
  • 146
Alexander Kleinhans
  • 5,950
  • 10
  • 55
  • 111
  • 1
    Please read: https://stackoverflow.com/a/730778/5378816 – VPfB Mar 15 '19 at 09:29
  • 1
    If you read the docs on those methods, you'll see that they raise exceptions. That's intentionally, so you have a chance to actually clean up your stuff, even if the program wants to quit. So why do you expect the program to quit w/o honoring the exception block? It is up to you to decide how to handle **individual** exceptions. – Mike Scotty Mar 15 '19 at 09:34
  • So a person that write's an `abort()` method as an emergency safety measure is suppose to know about any possible usage of that same piece of code (which is impossible)? That's a great reason never to use python for anything running machinery. – Alexander Kleinhans Mar 15 '19 at 09:36
  • 2
    If a person writes an ``abort()`` method, then whoever calls it (or calls code that may call it) is responsible for the behaviour that happens then. If the person calling it blindly catches all exception, it's that person's oversight. Python lets you handle individual exceptions. What if you're currently in a DB transaction and you need to undo some DB locking? If you just exit the program, the DB might stay locked forever. With exception handling, the caller has the chance to undo the locking and then quit gracefully. – Mike Scotty Mar 15 '19 at 09:38

3 Answers3

1

Continuing from what already has been explained by @Mike Scotty:

Picking one: (sys.exit())

sys.exit() raises an exception, namely SystemExit. That's why you land in the except-block.

Example:

import sys

try:
    sys.exit()
except:
    print(sys.exc_info()[0])

OUTPUT:

<class 'SystemExit'>

In depth:

import sys

try:
    sys.exit() # this always raises SystemExit
except SystemExit:
    print("sys.exit() worked as expected")
except:
    print("Something went horribly wrong") # some other exception got raised

OUTPUT:

sys.exit() worked as expected

Source

DirtyBit
  • 16,613
  • 4
  • 34
  • 55
0

Try CTRL+C while in the script, should do the trick

Tom
  • 116
  • 8
  • That's not the answer I was looking for. and also `CTRL+D` is better. – Alexander Kleinhans Mar 15 '19 at 09:28
  • Well, usually exit() works for me, but I'll upvote your question anyway in hopes someone can answer your question better. :) EDIT: CTRL+C stuck with me in the 10 years, always used it, it's just one of those things I guess – Tom Mar 15 '19 at 09:31
  • Yeah, what about a script that's running without user input (like on a server) and needs to get out of a loop? I'm looking for a programmatic answer, obviously. – Alexander Kleinhans Mar 15 '19 at 09:34
  • Then usually I just bash CTRL+Z,X,C,D until it stops xD – Tom Mar 15 '19 at 09:35
0

Well, I don't like the way Python handles this but I've found a solution and it's actually kill -9:

Disclaimer: Unfortunately, you have to start and run the process as root.

os.system("sudo kill -9 " + str(os.getpid()))
Alexander Kleinhans
  • 5,950
  • 10
  • 55
  • 111
  • You don't need to be root to kill own process (on Linux/Unix). so you don't need sudo. You don't need `os.system` with its large overhead just to send a signal, see `os.kill`. – VPfB Mar 15 '19 at 11:59