threads and exception in wxPython

Peter Hansen peter at engcorp.com
Wed Nov 3 10:35:02 EST 2004


Peter Hansen wrote:
> So the exception managed to get to the new thread, even though
> I called SetAsyncExc *before* the thread was even created.
> 
> Now _that's_ what I call "asynchronous". ;-)

Actually, it's what you call pilot error.  I was retrieving
threading._get_ident() in the thread initializer *before*
the thread was even started... stupid pilot.

Here's the final working version:

 >>> class T(threading.Thread):
...   def __init__(self):
...    threading.Thread.__init__(self)
...    self.stopped = False
...    self.start()
...   def run(self):
...    self.id = threading._get_ident()
...    try:
...      while not self.stopped:
...         time.sleep(0.1)
...    except:
...      print 'thread terminated!'
...
 >>> t = T()
 >>> t
<T(Thread-7, started)>
 >>> asyncexc = ctypes.pythonapi.PyThreadState_SetAsyncExc
 >>> exc = ctypes.py_object(ValueError)
 >>> asyncexc(t.id, exc)
1
 >>> thread terminated!

 >>> t
<T(Thread-7, stopped)>


-Peter



More information about the Python-list mailing list