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

A

Antoon Pardon

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,268
Latest member
AshliMacin

Latest Threads

Top