raising an exception in one thread from an other. (PyThreadState_SetAsyncExc)

Antoon Pardon apardon at forel.vub.ac.be
Tue Mar 15 04:58:24 EST 2005


I have been playing a bit with PyThreadState_SetAsyncExc. Now the
documentation abouth this call states the following:

| Returns the number of thread states modified; if it returns a number
| greater than one, you're in trouble, and you should call it again with
| exc set to NULL to revert the effect. 


Now I don't have trouble with it returning numbers greater that one.
However it does sometimes return zero. I have the impression this
happens when the thread in question has already finished. I have
two questions.

1) Should I call it again with exc set to NULL when it returned zero?

2) Would the following be a decent way to handle numbers returned
   greater than one? (Note the following is just to illustrate
   what I want to do, not functioning code)

  def raise_in_thread(thread_id, exception):

    Nr = PyThreadState_SetAsyncExc(thread_id, exception)
    while Nr > 1:
      PyThreadState_SetAsyncExc(thread_id, NULL)
      sleep(0.1)
      Nr = PyThreadState_SetAsyncExc(thread_id, exception)


What this basically does is when a number greater than one
is returned go in a loop to cancel the effect wait a short
time and try again.

-- 
Antoon Pardon



More information about the Python-list mailing list