MySQLdb best practices

I am currently developing a Python script that does a few things with some data pulled from a MySQL database. To access this data, I use the MySQLdb module .

This module follows the principles outlined in PEP 249, the Python API, and includes creating a connection object and a subsequent cursor object, which is used to iterate over the information.

Currently, in my project, I create a connection object anytime I need to make a MySQL read / write block, and then close it when done. However, I could easily pass the connection object to avoid re-opening / closing.

My question is: Given safety, resource management, etc., is a methodology open; read/write; close; repeat for the next read/write;better than an approach open; read/write; pass connection on for the next read/write;?

EDIT: Another context. This particular Python script is highly multithreaded. Does a complex process / thread environment affect which method more?

+5
source share
3 answers

, , , , - . , - . , . , ; , , . , , , , , .

, , , SQLAlchemy ( )

+1

, , . , , , , .

+1

, OurSQL MySQLdb.

, , , . , , - .

0

All Articles