Using PyThreadState_SetAsyncExc with PyEval_CallObject

spam_john at comcast.net spam_john at comcast.net
Thu Aug 11 12:17:19 EDT 2005


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




More information about the Python-list mailing list