changeset 386:846a59c10cf4

threads working but still slow startup
author catherine@DellZilla
date Fri, 02 Oct 2009 18:12:46 -0400
parents b9fedbfbec79
children e3dd9e4467d1
files sqlpython/schemagroup.py sqlpython/sqlpython.py
diffstat 2 files changed, 32 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/sqlpython/schemagroup.py	Fri Oct 02 14:10:57 2009 -0400
+++ b/sqlpython/schemagroup.py	Fri Oct 02 18:12:46 2009 -0400
@@ -1,12 +1,27 @@
-import gerald, re
+import gerald, re, datetime, threading
 
-def schemagroup(rdbms, connstr, connection, user):
+def get_schemagroup(rdbms, connstr, connection, user):
     gerald_connstring = connstr.split('/?mode=')[0].replace('//','/')
     if rdbms == 'oracle':
         childtype = OracleSchemaGroup            
     grp = childtype(gerald_connstring, connection, user)
     return grp
     
+class RefreshThread(threading.Thread):
+    def __init__(self, schemagroup, owner):
+        print 'beginning a thread for ' + owner
+        threading.Thread.__init__(self)
+        self.schemagroup = schemagroup
+        self.owner = owner
+        self.schema = self.schemagroup.schemas.get(self.owner)
+    def run(self):
+        if self.schema and (self.schema.age() < self.schema.refreshed):
+            self.schema.refreshed = self.schema.time()
+        else:
+            self.schema = self.schemagroup.childtype(self.owner, self.schemagroup)
+        self.schemagroup.schemas[self.owner] = self.schema
+        print 'finished thread for ' + self.owner
+            
 class SchemaGroup(object):
     def __init__(self, gerald_connstring, connection, user):
         self.connstring = gerald_connstring
@@ -14,16 +29,13 @@
         self.cursor = connection.cursor()
         self.user = user    
     def refresh(self):
+        # should trigger the beginning of a refresh
+        # or maybe check whether one is needed?
         now = self.schemas[self.user].time()
         for schemaname in self.all_schemanames():
-            if ((schemaname not in self.schemas) or 
-                (self.schemas[schemaname].age() < now) 
-                ):
-                self.schemas[schemaname] = self.childtype(schemaname,
-                                                          self)
-'SELECT owner, max(last_ddl_time) FROM all_objects GROUP BY owner'
-
-
+            schema_update_thread = RefreshThread(self, schemaname)
+            schema_update_thread.start()
+            
 class OracleSchema(gerald.OracleSchema):
     def __init__(self, schemaname, parent_group):
         self.parent_group = parent_group
@@ -31,14 +43,16 @@
                                      self.parent_group.connstring)
         self.refreshed = self.time()
     def time(self):
-        self.parent_group.cursor.execute('SELECT sysdate FROM dual')
-        return self.parent_group.cursor.fetchone()[0]
+        curs = self.parent_group.connection.cursor()
+        curs.execute('SELECT sysdate FROM dual')
+        return curs.fetchone()[0]
     def age(self):
-        self.parent_group.cursor.execute('''SELECT max(last_ddl_time) 
-                                            FROM   all_objects
-                                            WHERE  owner = :owner''',
-                                            {'owner': self.name})
-        return self.parent_group.cursor.fetchone()[0]            
+        curs = self.parent_group.connection.cursor()
+        curs.execute('''SELECT max(last_ddl_time) 
+                        FROM   all_objects
+                        WHERE  owner = :owner''',
+                        {'owner': self.name})
+        return curs.fetchone()[0]            
                 
 class OracleSchemaGroup(SchemaGroup):         
     childtype = OracleSchema
--- a/sqlpython/sqlpython.py	Fri Oct 02 14:10:57 2009 -0400
+++ b/sqlpython/sqlpython.py	Fri Oct 02 18:12:46 2009 -0400
@@ -102,7 +102,7 @@
         rdbms = eng.url.drivername
         conn  = {'conn': self.conn, 'prompt': self.prompt, 'dbname': eng.url.database,
                  'rdbms': rdbms, 'user': user, 'eng': eng, 
-                 'schemas': schemagroup.schemagroup(rdbms, arg,
+                 'schemas': schemagroup.get_schemagroup(rdbms, arg,
                                                     self.conn, user)}
         s = conn['schemas']
         s.refresh()