Using PyThreadState_SetAsyncExc with PyEval_CallObject

G

Guest

I am attempting to raise an exception in a thread currently calling
PyEval_CallObject from another thread using PyThreadState_SetAsyncExc.
The PyEval_CallObject is currently calling of a function with looks
like

def fun() :
import time
count = 0
while 1:
count = count + 1
time.sleep( 1 )

PyThreadState_SetAsyncExc is returning 1 which I beleive means that it
found the thread id, but PyEval_CallObject is never returning. Here's
the code I'm using to raise the exception -

PyEval_AcquireLock ();
PyThreadState_Swap( thread_state );
PyObject * exc = PyErr_NewException( "exit_exc", 0, 0 );
int count = PyThreadState_SetAsyncExc( thread_id, exc );
std::cout << "PyThreadState_SetAsyncExc returned " << count <<
std::endl;
if (count > 1) {
std::cout << "PyThreadState_SetAsyncExc returned > 1, all
bets are off!" << std::endl;
PyThreadState_SetAsyncExc( thread_id, NULL );
}
Py_DECREF( exc );
PyThreadState_Swap(0);
PyEval_ReleaseLock();

I know that the 'party line' is for my thread to check to see if it
should cancel. In my application the functions running in the threads
are completely end user defined so there's no way I can guarantee that
it will check to see if it should exit. Is it possible to cancel a call
to PyEval_CallObject? This is running on Python 2.4/Win32 but any
solution will have to be portable to PPC Linux.

Thanks-

John
 

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,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top