How do you register a function with all the correct handlers etc to be called when the Python script is exitted (successfully or not)?
I have tried:
@atexit.register
def finalise():
    '''
    Function handles program close
    '''
    print("Tidying up...")
    ...
    print("Closing")
...but this does not get called when the user closes the command prompt window for example (because @atexit.register decorated functions do not get called when the exitcode is non zero)
I am looking for a way of guaranteeing finalise() is called on program exit, regardless of errors.
For context, my Python program is a continually looping service program that aims to run all the time.
Thanks in advance
 
     
     
     
     
     
    