Santi Peñate-Vera Santi Peñate-Vera - 8 months ago 27
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


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?