title: Why are my threaded MySQLdb queries in Python slower than the same non-threaded queries?
body: <p>I am building a threaded class to run MySQL queries using Python and MySQLdb.  I don't understand why running these queries threaded is slower than running them non-threaded.  Here's my code to show what I'm doing.  </p>
  
  <p>First, here's the non-threaded function.</p>
  
  <pre><code>def testQueryDo(query_list):
  
      db = MySQLdb.connect('localhost', 'user', 'pass', 'db_name')
      cursor = db.cursor()
  
      q_list = query_list
      for each in q_list:
          cursor.execute(each)
          results = cursor.fetchall()
  
      db.close()
  </code></pre>
  
  <p>Here's my threaded class:</p>
  
  <pre><code>class queryThread(threading.Thread):
  
      def __init__(self, queue):
          threading.Thread.__init__(self)
          self.queue = queue
  
          self.db = MySQLdb.connect('localhost', 'user', 'pass', 'db_name')
          self.cursor = self.db.cursor()
  
      def run(self):
          cur_query = self.queue.get()
          self.cursor.execute(cur_query)
          results = self.cursor.fetchall()
          self.db.close()
          self.queue.task_done()
  </code></pre>
  
  <p>And here's the handler:</p>
  
  <pre><code>def queryHandler(query_list):
      queue = Queue.Queue()
  
      for query in query_list:
          queue.put(query)
  
      total_queries = len(query_list)
      for query in range(total_queries):
          t = queryThread(queue)
          t.setDaemon(True)
          t.start()
  
      queue.join()
  </code></pre>
  
  <p>I'm not sure why this threaded code is running slower.  What's interesting is that if I use the same code, only do something simple like addition of numbers, the threaded code is <em>significantly</em> faster.</p>
  
  <p>I understand that I must be missing something completely obvious, however any support would be much appreciated!</p>