patternpythonModerate
Should I use atexit to close db connection?
Viewed 0 times
closeshouldatexituseconnection
Problem
I just realized that I could turn:
into:
Which lets me keep the code for closing the database next to the opening, and saves me a line of code and an indentation level, which I like. Is there a reason why I shouldn't do this?
import MySQLdb
def main():
db = MySQLdb.connect( .. )
try:
while True:
do_thingy()
finally:
db.close()into:
import MySQLdb, atexit
def main():
db = MySQLdb.connect( .. )
atexit.register( db.close )
while True:
do_thingy()Which lets me keep the code for closing the database next to the opening, and saves me a line of code and an indentation level, which I like. Is there a reason why I shouldn't do this?
Solution
Firstly, an alternative:
The database connection will be closed after the with block even in the case of exceptions.
The primary problem created by atexit is in any attempt to reuse that code in question. It will only release the database connection when the entire program exits. That's fine in the case where this function is your entire program. But at some later point that my cease to be the case.
If you really want the database connection to be closed at the end of the function, its clearest if you write code that does that rather then assuming that this is the only function called in the program.
There really is not much of saving to use the atexit route. I think the clarity in the "with" method is worth the extra level of indentation. Additionally, you can use this same technique anywhere in code not just in a main function.
import MySQLdb, contextlib
def main():
db = MySQLdb.connect( .. )
with contextlib.closing(db):
do_thingy()The database connection will be closed after the with block even in the case of exceptions.
The primary problem created by atexit is in any attempt to reuse that code in question. It will only release the database connection when the entire program exits. That's fine in the case where this function is your entire program. But at some later point that my cease to be the case.
If you really want the database connection to be closed at the end of the function, its clearest if you write code that does that rather then assuming that this is the only function called in the program.
There really is not much of saving to use the atexit route. I think the clarity in the "with" method is worth the extra level of indentation. Additionally, you can use this same technique anywhere in code not just in a main function.
Code Snippets
import MySQLdb, contextlib
def main():
db = MySQLdb.connect( .. )
with contextlib.closing(db):
do_thingy()Context
StackExchange Code Review Q#1274, answer score: 14
Revisions (0)
No revisions yet.