This is the scheme i use for sqlobject type sql operations, is
sqlalchemy better I've been hearing abunch about it lately I'll look at
it. I think that sqlobject is stable though. This code works for a
few hours at least I think.
Usage
conpool.getLock() #lock
conpool.getConnection()
conpool.relLock() #hub uses it own locking or is thread safe
hub.begin()
selectedURI = URI.select(URI.q.address == site)
selectedURIList = list(selectedURI)
if len(selectedURIList) == 0:
time_=time()
d = str(lines)
c = URI(address=site,data=d,time=time_)
hub.commit()
else:
c = selectedURIList[0]
hub.end()
#this goes in the model.py file
from sqlobject import *
from turbogears.database import AutoConnectHub
from sqlobject.mysql.mysqlconnection import MySQLConnection
import time
import threading
import MySQLdb
threadlocker = threading.Lock()
hub = AutoConnectHub()
def connect(threadIndex):
""" Function to create a connection at the start of the thread """
hub.threadConnection =
MySQLConnection('db','u','p','localhost',3306) #AutoConnectHub()#
def synch(func,*args):
def wrapper(*args):
try:
threadlocker.acquire()
return func(*args)
finally:
threadlocker.release()
return wrapper
class ConnectionPool:
def __init__(self, timeout=100, checkintervall=10):
self.timeout=timeout
self.lastaccess={}
self.connections={}
self.checkintervall=checkintervall
self.lastchecked=time.time()
self.lockingthread=None
def getConnection(self):
global hub
tid=threading._get_ident()
try:
con=self.connections[tid]
except KeyError:
print 'key error re-connecting'
self.connections[tid]=MySQLConnection('db','u','p','localhost',3306)
#hub.threadConnection
con = self.connections[tid]
self.lastaccess[tid]=time.time()
if (self.lastchecked + self.checkintervall) < time.time():
self.cleanUp()
return con
def getLock(self):
while not self._checklock():
#print 'in check lock'
time.sleep(0.02)
self.lockingthread=threading._get_ident()
getLock=synch(getLock)
def relLock(self):
self.lockingthread=None
def _checklock(self):
if self.lockingthread == threading._get_ident() or
self.lockingthread is None:
return True
return False
def cleanUp(self):
dellist=[]
for con in self.connections:
if self.lastaccess[con] + self.timeout < time.time():
self.connections[con].close()
#del self.lastaccess[con]
dellist.append(con)
for l in dellist:
print 'deleted connections'
del self.connections[l]
def kill(self):
tid=threading._get_ident()
dellist=[]
self.connections[tid].close()
#del self.lastaccess[tid]
dellist.append(tid)
for l in dellist:
print 'deleted connection'
del self.connections[l]
conpool = ConnectionPool()
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/turbogears
-~----------~----~----~----~------~----~------~--~---