Blog available for sell
This blog is available for sale. Please 'contact us' if interested.
Advertise with us
tutorial tips   0   54898
atexit with example

There is an option in python where you can execute a function when the interpreter terminates.

Consider this below example, save the code in a file and run it.


import atexit


def aa_atexit():
print('atexit of this aa called')


atexit.register(aa_atexit)
print('started')


$ python3 atexit_example.py


Output:

started
atexit of this aa called

aa_atexit is called just before the interpreter terminates.


As per python documentation:

The atexit module defines functions to register and unregister cleanup functions. Functions thus registered are automatically executed upon normal interpreter termination. atexit runs these functions in the reverse order in which they were registered; if you register AB, and C, at interpreter termination time they will be run in the order CBA.



Consider the below example:

Hence we can register functions to perform cleanup tasks in with atexit. Also, these functions are executed FILO/LIFO (first in last out or last in first out) order.


import atexit


class CC:
def __init__(self):
atexit.register(self.gg)

def gg(self):
print('atexit of cc called')


class BB:
def __init__(self):
atexit.register(self.hh)

def hh(self):
print('atexit of bb called')

def bb(self):
CC()
print('bb called')


def aa_atexit():
print('atexit of this aa called')


atexit.register(aa_atexit)
BB().bb()
print('started')


Here we registered the aa_atexit function first, then created an object of BB class where we registered a function with atexit for BB class and then when function bb() of BB class was called, we registered a function with atexit for CC class by initializing the CC class.

So the sequence of functions registered with atexit is aa_atexit(), BB.hh() and CC.gg(). When we run this program with a python interpreter, we know that CC.gg() should be called first and then BB.hh() and at last aa_atexit(). Let's run the code and see.


output:

rana@brahma$ python atexit_experiment.py 
bb called
started
atexit of cc called
atexit of bb called
atexit of this aa called


Just like how we can register the function with atexit, we can unregister the functions as well.


Use of atexit register:

As discussed, we can use the atexit registered function for cleanup purposes. There are other uses as well as described in this python bug mail.


https://mail.python.org/pipermail/python-bugs-list/2009-January/069209.html






tutorial tips   0   54898
0 comments on 'Atexit With Example'

Related Articles:
Improve Your Python Practices: Debugging, Testing, and Maintenance
improving your python skills, debugging, testing and practice, pypi...
How to start with Python Programming - A beginner's guide
starting with python programming, how to start learning python programming, novice to expert in python, beginner to advance level in python programming, where to learn python programming, become expert in python programming...
Adding Robots.txt file to Django Application
Adding robots.txt file in your Django application, Easiest way to add robots.txt file in Django, Django application robots.txt file, Why should you add robots.txt file in your Django Application,...
Server Access Logging in Django using middleware
Creating access logs in Django application, Logging using middleware in Django app, Creating custom middleware in Django, Server access logging in Django, Server Access Logging in Django using middleware...
DigitalOcean Referral Badge

© 2024-2025 Python Circle   Contact   Sponsor   Archive   Sitemap