python psycopg2 doesn't update database when using multithreading

391 views Asked by At

i have this code:

   while i != 10:   ##only for writing query's##
      print i
      db_connection.insert(i, psycopg2.connect(dbname='routing', user='root_db', password='root', host='localhost'))
      db_connection[i].autocommit = True
      queue.put(Db_Connection(db_connection[i], db_connection[i].cursor()))
      i = i+1
   ##only for reading query's##
   while i != 10:
      db_connection.insert(i, psycopg2.connect(dbname='routing', user='root_db', password='root', host='localhost'))
      db_connection[i].autocommit = True
      queue_read.put(Db_Connection(db_connection[i], db_connection[i].cursor()))
i = i+1 

def read(dport):
   obj = queue_read.get()
   conn = obj.db_connection
   cursor_db = obj.db_cursor
   try:
       cursor_db.execute("SELECT * FROM routing_table where port=%s", (str(dport),))
       results = cursor_db.fetchall()
       for rows in results:
          if rows:
            return rows
       return -1
   ##prints the exception
   except:
       print sys.exc_info()[0]
       return -1
   finally:
       queue.put(obj)


def write(port , pkt):
   obj = queue.get()
   conn = obj.db_connection
   cursor_db = obj.db_cursor
   str_port = str(port)
   str_src_port = str(pkt[IP].sport)
   srcip = str(pkt[IP].src)
   srcmac = str(pkt[Ether].src)
   try:
       if pkt.haslayer(TCP):
           cursor_db.execute("""INSERT INTO routing_table (port,srcip,srcport,srcmac,protocol)
         VALUES (%s,%s,%s,%s,%s)""" ,(str_port, srcip, str_src_port, srcmac, 'TCP'))
       else:
            cursor_db.execute("""INSERT INTO routing_table (port,srcip,srcport,srcmac,protocol)
         VALUES (%s,%s,%s,%s,%s)""" ,(str_port, srcip, str_src_port, srcmac, 'BLA'))
   ##prints the exception
   except:
       print sys.exc_info()[0]
       conn.rollback()
   finally:
      queue.put(obj)

   class Db_Connection():
     db_connection = '0'
     db_cursor = '0'

     def __init__(self,connection,cursor):
       self.db_connection = connection
       self.db_cursor = cursor

this code is basically read and write to the database while using queue and multi threading , what my problem is that when i use a lot of threads the read doesn't work well anymore (query return null for stuff that excites)? Do someone have any idea why ? i thought it might be because the read happens before the write finish updating the database but how can i solve this problem?

i changed my method to work with queues and file and not sql , so i could handle my self all the sync mechanism.

0

There are 0 answers