title: Python threading did not execute all threads which created
body: <p>I have a python script, so I use threading module in python to execute concurrently.</p>
  
  <pre><code>class check_bl(threading.Thread):
      def __init__(self, ip_client, reverse_ip, key, value):
          threading.Thread.__init__(self)
          self.ip_client = ip_client
          self.reverse_ip = reverse_ip
          self.key = key
          self.value = value
  </code></pre>
  
  <p>def run(self):
          db = MySQLdb.connect('localhost', 'mytable', 'user', 'mytable')
          cursor = db.cursor()
          query = "dig +short " + str(reverse_ip) + "." + key
          try:
              output = subprocess.check_output(query, shell=True)
              output_edited = output.strip()
          except:
              output_edited = "Timeout occured"</p>
  
  <pre><code>    if output_edited in value:
          try:
              cursor.execute() # execute INSERT command
              db.commit()
          except:
              db.rollback()
          db.close()
      else:
          try:
              cursor.execute() # execute INSERT command
              db.commit()
          except:
              db.rollback()
          db.close()
  </code></pre>
  
  <p>for ip_client in IPNetwork(range_ip_client):
      ip_client = ....
      reverse_ip = ...</p>
  
  <pre><code>for key, value in my_dictionary.items():
      myThread = check_bl(ip_client, reverse_ip, key, value)
      myThread.start()
      threadList.append(myThread)
  
  for t in threadList:
  t.join()
  </code></pre>
  
  <p>The problem is each some key, value in my_dictionary did not execute(not INSERT to database, although some key, value execute more than one time).
  My dictionary has 30 items, but just 10 - 20 of them execute. If I insert join() like 2 example below, all my dictionary item() execute but at the same time, just one thread execute.
  Is there something wrong for every threads created, it create an database connection. So database can't handle it.</p>
  
  <pre><code>for key, value in my_dictionary.items():
      myThread = check_bl(ip_client, reverse_ip, key, value)
      myThread.start()
      threadList.append(myThread)
  
  for t in threadList:
      t.join()
  </code></pre>
  
  <p>or</p>
  
  <pre><code>for key, value in my_dictionary.items():
      myThread = check_bl(ip_client, reverse_ip, key, value)
      myThread.start()
      t.join()
  </code></pre>
  
  <p>Thanks Dksj, my problem is solved. Maybe something wrong with class method, just need to define a function.</p>
  
  <pre><code>def check_bl(ip_client, reverse_ip, key, value):
      db = MySQLdb.connect('localhost', 'mytable', 'user', 'mytable')
      cursor = db.cursor()
      query = "dig +short " + str(reverse_ip) + "." + key
      try:
          output = subprocess.check_output(query, shell=True)
          output_edited = output.strip()
      except:
          output_edited = "Timeout occured"
  
      if output_edited in value:
          try:
              cursor.execute() # execute INSERT command
              db.commit()
          except:
              db.rollback()
          db.close()
      else:
          try:
              cursor.execute() # execute INSERT command
              db.commit()
          except:
              db.rollback()
          db.close()
  
  for ip_client in IPNetwork(range_ip_client):
      ip_client = ....
      reverse_ip = ...
      for key, value in my_dictionary.items():
          myThread = check_bl(ip_client, reverse_ip, key, value)
          threadList.append(myThread)
  
  [t.start() for t in threadList]
  
  [t.join() for t in threadList]
  </code></pre>