2

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

5 Answers 5

3

I don't think it can be done in pure Python. From documentation:

Note: the functions registered via this module are not called when the program is killed by a signal not handled by Python, when a Python fatal internal error is detected, or when os._exit() is called.

I think you may find this useful: How to capture a command prompt window close event in python

0

Have you tried to just catch all kinds of exceptions in your main function or code block?

1
  • Just tried it, unfortunately it doesn't throw an error when the user closes the command prompt, so it doesn't handle that exit. Thanks for the thought.
    – James
    Commented Dec 18, 2012 at 11:33
0

For context, my Python program is a continually looping service program that aims to run all the time.

This is very hard to get right (see How do you create a daemon in Python?). You should use a library like http://pypi.python.org/pypi/python-daemon/ instead.

0

This one should work

works both on Ctrl-C and when the assertion fails. Maybe you can use a similar construct and pack it as a decorator, or whatever.

def main():
  print raw_input('> ')
  # do all your stuff here  

if __name__ == '__main__':
  try:
    main()
  finally:
    print 'Bye!'
3
  • Okay, not sure I understand how this is to work - could you elaborate a little? Are you saying that assert should throw an error when the user closes the command prompt, which is then caught in the try:...catch: hence allowing me to handle the exit?
    – James
    Commented Dec 18, 2012 at 11:48
  • @James: sorry for confusion, I edited the question. assert 0 was just an example, that it will catch an error and print "Bye". Also, when you press Ctrl-C, the finally part will be executed.
    – Jakub M.
    Commented Dec 18, 2012 at 12:47
  • 1
    Sorry, I'm afraid that doesn't catch the error of a user closing the command prompt running the Python script.
    – James
    Commented Dec 18, 2012 at 13:10
-1
atexit.register(func)

func = lambda x: x

Use http://docs.python.org/2/library/atexit.html

3
  • Yup, not reliable as described above.
    – James
    Commented Dec 18, 2012 at 11:44
  • When user closes interpreter probably it is called os.exit() instead of sys.exit() , Correect? Commented Dec 18, 2012 at 11:47
  • Yup, quite possibly. I need to handle all exiting.
    – James
    Commented Dec 18, 2012 at 11:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.