Python MySQL Connection Pool

I have updated this library and changed its name. To download the new version go to http://code.google.com/p/pysqlpool/. Read more on the changes

Recently I have been working on a Python application that required the use of connection pool to help speed up the processing time of the whole application. As making a new connection to the Database takes a lot of time. A connection pool is one of the best things to do to help speed this up.

I started to look all over the net and really never found out any good ideas for one that would support threading. So I set out to create my own. So here is what I came up with. MySQLPool Source

The MySQLPool class is the primary class for managing all the connections. It is never touched outside of the MySQL class. The MySQLPool class uses The Borg Pattern to store all the connections between all the instances and threads. As well as uses the Threading.Condition() to lock each connection as well as the connection dictionary.

The MySQL class is the primary class used by all the queries though out each thread and class. Each time you need to run a query. You create a new instance of MySQL passing it all your connection information in the form of a dictionary. Then call the query function with your SQL and replacement arguments (Just as if you where using the native cursor.execute() function).

When use correctly all your threads will be able to share the same connections and help speed things up.

Example:

from MySQLPool import *

conn= {“host”:”localhost”,”user”:”root”,”passwd”:”python”,”db”:”companies”}
query = MySQL(conn, True)
query.Query(“select * from company where company_id = %s”, (self.company_id))

if (int(query.rowcount) > 0):
   for row in query.record:
      print row["company"]

Source Distro Package: MySQLPool-1.0.zip
Linux Tar package to come soon.

This entry was posted in Personal Projects. Bookmark the permalink.

One Response to Python MySQL Connection Pool

  1. NerdyNick says:

    If you are looking to use this package. I have replaced it with a more up to date and Open Sourced version now called PySQLPool. You can read all about it at PySQLPool Post

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>