Threads and sys.excepthook

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jesus Rivero - (Neurogeek)

    #1

    Threads and sys.excepthook

    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_ne w(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.DBManag er 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_ne w(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)
  • Jonathan Ellis

    #2
    Re: Threads and sys.excepthook

    Jesus Rivero - (Neurogeek) wrote:[color=blue]
    > 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_ne w(test,
    > (name,)) #(1) part, then it does it all perfectly.[/color]

    Looks like the interpreter is shutting down before all the exception
    processing finishes.

    -Jonathan

    Comment

    Working...