#-*-Coding: UTF-8 -*-
# Author: newbie
_ Author _ = 'admin'
# Disable callback for the atexit program in The py standard library
Import atexit
# It is used to call a function when the registration program is closed normally. the SYS module also provides a hook, SYS. exitfunc, but only one function can be registered here. The aatexit Registry has multiple modules and libraries used at the same time.
# Example 1: register a function through register ()
Def all_done ():
Print 'all _ DONE ()'
Print 'registering'
Atexit. Register (all_done)
Print 'registering'
# Because seed does not do other work, all_done () is called immediately ()
# You can also register multiple functions and pass parameters to the registered functions. This is useful for properly disconnecting the database and deleting temporary files, you do not need to maintain a special list for translation resources. You can register a separate cleanup function for each resource.
Def clear1 (name ):
Print 'clear (% s) '% name
Atexit. Register (clear1, 'abc ')
Atexit. Register (clear1, '20140901 ')
Atexit. Register (clear1, 'two ')
# The exit function is called in the reverse order of registration. This method completes module cleaning in the reverse order of module import (correspondingly, it registers the atext function order), which reduces dependency conflicts.
# Under what circumstances do I not call the atext function?
"""
If any of the following conditions is met, the atext function is called in one action.
1: The program is terminated due to a signal.
2: OS. _ exit ()
3: a fatal error is detected in the interpreter.
"""
#1 signal
Import OS, signal, subprocess, time, sys
Proc = subprocess. popen (r 'G: \ project \ x1.py ')
Print 'parent: pausing before seding signal 1 ...'
Time. Sleep (1)
Print 'parent: pausing before seding Signal 2 ...'
OS. Kill (Proc. PID, signal. sigterm)
# An atext callback is recommended for the subroutine, and then sleep until the signal arrives.
#2
Def not_called ():
Print 'not called ()'
Print 'atexit handler'
SYS. stdout. Flush ()
Atexit. Register (not_called)
Print 'signal'
SYS. stdout. Flush ()
Time. Sleep (5)
# Subprograms do not print messages nested in not_called ()
# If the program uses OS. _ exit (), it will not call the atextit callback.
Def call ():
Print 'call ()'
Print 'registering'
Atexit. Register (call)
Print 'register'
Print 'exit ....'
OS. _ exit (0)
# If you want to ensure that the callback runs, you can run it outside the statement or call SYS. Exit () to abort the program.
Def donw ():
Print 'down ()'
Print 'registering'
Atexit. Register (donw)
Print 'register'
Print 'exit ....'
SYS. Exit ()
# Handling exceptions
# The traceback generated in the atexit callback is printed to the console. When an exception occurs, it is re-recruited as the last error message of the program.
Def exit1 (MESS ):
Raise runtimeerror (MESS)
Atexit. Register (exit1, 'one ')
Atexit. Register (exit1, 'two ')
# The registration sequence controls the execution sequence. If an error in a callback introduces an error in another callback (the more you register, the more you call it ), the final error message may not display the most useful error information for the user.
# Official standard library address: https://docs.python.org/2.7/library/atexit.html? Highlight = atexit # module-atexit
Disable callback of the atexit program in The py standard library