title: How to use PostgreSQL in multi thread python program
body: <p>I am using psycopg2 (2.6) connect to PostgreSQL database in a multi-threading python program.</p>
  
  <p>When queue size in program increase, select queries get error "no results to fetch", but inserts records to db works very well. </p>
  
  <p>example code:</p>
  
  <pre><code>class Decoders(threading.Thread):
      def __init__(self, queue):
          threading.Thread.__init__(self)
          self.queue = queue
  
      def run(self):
          self.decode()
  
      def decode(self):
          queue = self.queue
          db = Database()
          while queue.qsize() &gt; 0:    
              # calling db methods, just an example
              temp = queue.get()
              db.select_records()
              db.insert_record(temp)
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>Decoders(queue).start()
  Decoders(queue).start()
  </code></pre>
  
  <p>note: I don't have this problem with multiprocessing.</p>
  
  <p>Edit:</p>
  
  <p>When I start only one thread, the program doesn't have any problem.</p>
  
  <p>database class:</p>
  
  <pre><code>class Database:
      db = object
      cursor = object
  
      def __init__(self):
          self.db = connect(host=conf_hostname,
                            database=conf_dbname,
                            user=conf_dbuser,
                            password=conf_dbpass,
                            port=conf_dbport)
          self.db.autocommit = True
          self.cursor = self.db.cursor()
  
      def select_records(self):
          self.cursor.execute(simple select)
          return self.cursor.fetchall()
  
  
      def insert_record(self, temp):
          # insert query
  </code></pre>