M
markus.mj
Hi,
I am looking for help with following problem. I scripted threaded database query, with session open per thread, and queries delivered through queue. Every open DB session must be closed with "abort" or "commit", however on which event should I trigger the DB disconnect? Ideally it would close the DBas the thread class gets deconstructed, but "__del__" does never fire. Howwould you solve the problem? Here an non working example (bended from IBM developerWorks tutorial) that explains the situation:
<code>
multi_query = ["query1","query2","query3","query4","query5","query6"]
queue = Queue.Queue()
class ThreadSql(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
#Open database connection instance
self.session = DbConnect()
def run(self):
while True:
#grabs query from queue
query = self.queue.get()
#Fire query and print result
print self.session.SQL(query)
#Queue job is done
self.queue.task_done()
# THIS PART IS NOT WORKING
def __del__(self):
#Disconnect Database session and commit or abort transactions
self.session.Disconnect(<abort, commit>)
print "The End"
#---------------------------------
for i in range(5):
t = ThreadUrl(queue)
t.setDaemon(True)
t.start()
#Fill the queue
for single_query in multi_query:
queue.put(single_query)
#Wait until query is empty and finish
queue.join()
</code>
Thank you for any idea!
Markus
I am looking for help with following problem. I scripted threaded database query, with session open per thread, and queries delivered through queue. Every open DB session must be closed with "abort" or "commit", however on which event should I trigger the DB disconnect? Ideally it would close the DBas the thread class gets deconstructed, but "__del__" does never fire. Howwould you solve the problem? Here an non working example (bended from IBM developerWorks tutorial) that explains the situation:
<code>
multi_query = ["query1","query2","query3","query4","query5","query6"]
queue = Queue.Queue()
class ThreadSql(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
#Open database connection instance
self.session = DbConnect()
def run(self):
while True:
#grabs query from queue
query = self.queue.get()
#Fire query and print result
print self.session.SQL(query)
#Queue job is done
self.queue.task_done()
# THIS PART IS NOT WORKING
def __del__(self):
#Disconnect Database session and commit or abort transactions
self.session.Disconnect(<abort, commit>)
print "The End"
#---------------------------------
for i in range(5):
t = ThreadUrl(queue)
t.setDaemon(True)
t.start()
#Fill the queue
for single_query in multi_query:
queue.put(single_query)
#Wait until query is empty and finish
queue.join()
</code>
Thank you for any idea!
Markus