In-depth understanding of the atexit module and pythonatexit module in python

Source: Internet
Author: User

In-depth understanding of the atexit module and pythonatexit module in python

Introduction to the atexit Module

The python atexit module defines a register function, which is used to register an exit function in the python interpreter. This function is automatically executed when the interpreter ends normally and is generally used to clean up resources. Atexit executes these functions in the reverse order of registration. For example, when registering A, B, and C, the interpreter stops running C, B, and.

Note: If the program is not a normal crash, oros._exit()Exit. The registered exit function will not be called.

Official documents: https://docs.python.org/3.5/library/atexit.html

Register the exit function

atexit.register(func, *args, **kargs)

Use func as the function to be executed upon termination. Any optional parameter to be passed to func must be passed as a parameterregister(). You can register the same functions and parameters multiple times.

When the program exits, the registered function is called in the first and later order. If the exit function throws an exception during execution, atexit prints the exception information and continues executing the next Callback until all exit functions are executed, it will throw the final exception.

Example

Through the decoration device:

#!/usr/bin/env pythonfrom atexit import registerdef main(): print('Do something.')@registerdef _atexit(): print('Done.')if __name__ == '__main__': main()

Non-decorators:

#!/usr/bin/env pythonfrom atexit import registerdef main(): #pass print('XX')def goodbye(name, adjective): print('Goodbye, %s, it was %s to meet you.' % (name, adjective)) register(goodbye, 'Donny', 'nice')# or:# register(goodbye, adjective='nice', name='Donny')if __name__ == '__main__': main()

Delete and exit the function [not commonly used]

> atexit.unregister(func)>

Delete func from the list of functions that run when the interpreter is disabled. Callunregister()When the interpreter is disabled, func will not be called even if it is registered multiple times. If func has not been registeredunregister()Nothing can be done.

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.