Threads and sys.excepthook

Jesus Rivero - (Neurogeek) jrivero at python.org.ve
Tue Mar 28 17:02:21 CEST 2006


Hello guys,

   I have this problem and i don't know any workarounds yet. Im
implementing a DB Connection pool, which initially creates 20
connections and keep them in a dictionary. It also implements a method
for allowing external method/classes to get a connection from that pool.
he main issue is that, from a testing app, I create a loop like this to
test the pool:

import thread
import sys
import seen #this is my module

DB = seen.DBManager()

def test(name):
   try :
      idc,conn = DB.get_conn()
      print "Thread: ",name," -- ",DB," ID: ",idc

   except :
      print sys.exc_info()[0]
      print sys.exc_info()[1]
      print "ERROR IN HERE"
   return

for i in xrange(20) :
   try:
      name = 'THREAD' + str(i)except:
      thread.start_new(test, (name,)) #(1)
   except:
      print 'An Exception Ocurred'
      print sys.exc_info()[0]
      print sys.exc_info()[1]

 
But when i run this code, The code works fine the first 5 times. the
rest, all i get is this:

SIZE:  20
Thread:  THREAD0  --  <seen.DBManager object at 0x403ca7ec>  ID:  0
SIZE:  19
Unhandled exception in thread started by
Error in sys.excepthook:

Original exception was:
Unhandled exception in thread started by
Error in sys.excepthook:

Original exception was:
Unhandled exception in thread started by
Error in sys.excepthook:

Original exception was:
Unhandled exception in thread started by
Error in sys.excepthook:

Original exception was:

And if put a time.sleep(1) after the       thread.start_new(test,
(name,)) #(1) part, then it does it all perfectly.

I dont know what is causing the problem, maybe the threads are spawning
too fast? OS issues?? python issues??

Im running Gentoo GNU/Linux and Python2.4

Thanks in advance,

    Jesús Rivero - (Neurogeek)



More information about the Python-list mailing list