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 …[Read more]