Easy Python: multi-threading MySQL queries

There are many times when writing an application that single threaded database operations are simply too slow. In these cases it’s a matter of course that you’ll use multi-threading or forking to spawn secondary processes to handle the database actions. In this simple example for Python multi-threading you’ll see the how simple it is to improve the performance of your python app.

#!/usr/bin/python
## DATE: 2010-08-30
## AUTHOR: Matt Reid
## WEBSITE: http://themattreid.com
## LICENSE: BSD http://www.opensource.org/licenses/bsd-license.php
## Copyright 2010-present Matt Reid

from __future__ import division
from socket import gethostname;
import threading
import sys
import os
import MySQLdb

class threader(threading.Thread):
    def __init__(self,method):
        threading.Thread.__init__(self)
        self.tx =
        self.method = method
    def run(self):
        run_insert()

def run_insert():
    sql = "INSERT INTO table (`id`,`A`,`B`,`C`) VALUES (NULL,'0','0','0');")
        try:
            cursor.execute(sql)
            db.commit()
        except:
            print "insert failed"

def init_thread(): backgrounds = []
    for db in connections:
       logger("Spawning thread: %s"%(db),"d")
       quant = tx / THREADS
       background = threader(method,quant,db)        
       background.start()
       backgrounds.append(background)
    for background in backgrounds:
       background.join()

def main():
    try:
        init_thread()
    except:
        print "failed to initiate threads"

    sys.exit(0)

if __name__ == "__main__":
    mysql_host = "localhost" #default localhost
    mysql_pass = "pass" #default dbbench
    mysql_user = "user" #default dbbench
    mysql_port = 3306 #default 3306
    mysql_db = "schema" #default dbbench
    threads = 4 #must be INT not STR #create connection pool

    connections = []
    for thread in range(THREADS):
      try:
       connections.append(MySQLdb.connect(host=mysql_host, user=mysql_user, passwd=mysql_pass, db=mysql_db, port=mysql_port))
      except MySQLdb.Error, e:
       print "Error %d: %s"%(e.args[0], e.args[1])
       sys.exit (1)

    main()
    
Social tagging: > > > > >

4 Responses to Easy Python: multi-threading MySQL queries

  1. Hey, I’ve read this article on Planet MySQL recently and bookmarked it. I’ve finally opened sqlexamples.org to the public which I’m slowly want to introduce to the open source community. What do you personally think about publishing such interesting snippets on sqlexamples.org? We want to build an archive of free (as in free speech) sql and nosql related examples of all kinds. To make this dream come true, we also give users a fair chance to get something back for their work. This is seriously no spam and no fake:

    http://sqlexamples.org/content/new-star-born-sqlexamplesorg-goes-online

    Thank you very much!

  2. admin says:

    Sounds like a fun project. I’ll keep it in mind for future posts. :)

  3. Jay Pipes says:

    You will run into performance problems with the Python threading module because of the GIL. I’d recommend looking at either Eventlet for non-blocking asynchronous calls (eventlet.GreenPool and similar) or using the multiprocessing Python 2.6+ module.

    Cheers,
    jay

  4. netmouse says:

    self.tx= ???

Trackbacks/Pingbacks

  1. Multi-threading MySQL queries in Python (no replies) | Forge network

Leave a Reply

*