Santi Peñate-Vera Santi Peñate-Vera - 1 year ago 101
Python Question

Stop python script without killing the python process

I would like to know if there is a way of programatically stopping a python script execution without killing the process like we do with this code:

import sys

It would be the code equivalent to Ctrl+c

Answer Source

Define your own exception,

class HaltException(Exception): pass

and wrap the script in

    # script goes here

    # when you want to stop,
    raise HaltException("Somebody stop me!")

except HaltException as h:
    # now what?
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download