Threading.Lock() question

2006-03-09 Thread Alvin A. Delagon
I have a multithreaded application that spawns threads which query a 
database server. During stress test I encountered some threads failing 
due lost connection errors and sometimes the actual script itself dies 
due to a Segmentation Fault. I realized that it might be a deadlock 
situation so I applied a lock mechanism but after another stress test, 
some threads are still failing. Here's a rough sketch of my script:

class Process(threading.Thread):
def __init__(self,lock,query)
   self.lock = lock
   self.query = query
   threading.Thread.__init__(self)
def run(self):
   ''' Some data processing code here '''
   self.lock.acquire()
   result = cursor.execute(query)
   self.lock.release()

class Send(Request__POA.Send):
   def push(self,query,...some args here):
  lock = threading.Lock()
  Process(lock,query).start()

db = MySQL().connect()
cursor = db.make_cursor()

The class Send is being called by a CORBA client. The script is actually 
a CORBA server which in the future would serve a high traffic of 
requests. Is my usage is locking correct or am I doing something stupid 
in the code? Thanks is advance!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Threading.Lock() question

2006-03-09 Thread Alvin A. Delagon
I think I just found out my problem. How stupid of me, I should've 
created an instance of lock in the class Process instead of class Send. 
Good Heaven's python's threading rocks! I stress tested the server 
script having two clients sending requests on a while 1 loop without 
even a time.sleep() and yet it still runs! ^_^

class Process(threading.Thread):
   lock = threading.Lock() # Lock instance should be placed here
   def __init__(self,query)
  self.query = query
  threading.Thread.__init__(self)
   def run(self):
  ''' Some data processing code here '''
  Process.lock.acquire()
  cursor.execute(query)
  Process.lock.release()

class Send(Request__POA.Send):
   def push(self,query,...some args here):
  Process(query).start()

db = MySQL().connect()
cursor = db.make_cursor()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Threading.Lock() question

2006-03-09 Thread Alvin A. Delagon
@ Dennis

Thanks for the quick heads up! You're right! I noticed the bug on my 
script just after sending out my question. Yes, I'm using MySQLdb but I 
did a wrapper for it to further simplify my script that why the sample 
script I wrote is a little bit different. Thanks again! Python rules!!! ^_^
-- 
http://mail.python.org/mailman/listinfo/python-list