embedding: forcing an interpreter to end

pdectm at yahoo.com pdectm at yahoo.com
Mon Dec 20 00:05:13 EST 2004


I'm trying to prototype an application which runs multiple python
scripts, each in its own interpreter and OS thread.  I've not been able
to forceable stop a script which does not respond to a request to stop.

My thought was to simply call:

PyThreadState_Clear(xxx);
PyThreadState_Delete(xxx);  // Fatal Python error: no last thread
Py_EndInterpreter(xxx);

But, this doesn't work.  The call to delete causes a fatal error.  If I
skip the call to delete, the call to EndInterpreter() ends but seg
faults abound afterwards.  A small sample follows.

I've googled my heart out and reread the threading API countless times.
Any suggest or hints would be greatly appreciated.

----
#include "Python.h"

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

PyThreadState *gtstate;

void * mythread( void *none )
{
PyEval_AcquireLock();
gtstate = Py_NewInterpreter();

PyRun_SimpleString( "import time\nwhile 1:\n\tprint \"I won't
stop\"\n\ttime.sleep(1.0)\n" );

printf( "Interpreter was shutdown ... yeah \n" );

PyEval_ReleaseLock();
}

int main(int argc, char *argv[] )
{
pthread_t thread;
PyThreadState *tstate;

Py_Initialize();
PyEval_InitThreads();
tstate = PyEval_SaveThread();

pthread_create( &thread, (pthread_attr_t *)NULL, mythread, NULL );

sleep(3);

/* Make that pesky script die */
printf( "die ... \n" );
PyEval_AcquireLock();

PyThreadState_Clear(gtstate);
//PyThreadState_Delete(gtstate);  // Fatal Python error:
Py_EndInterpreter: not the last thread

PyThreadState_Swap(gtstate);
Py_EndInterpreter(gtstate);

PyEval_ReleaseLock();
sleep(3);                         // Segmentation fault
PyEval_AcquireThread(tstate);
   Py_Finalize();

   return(0);
}




More information about the Python-list mailing list