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_C lear(xxx);
PyThreadState_D elete(xxx); // Fatal Python error: no last thread
Py_EndInterpret er(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_AcquireL ock();
gtstate = Py_NewInterpret er();
PyRun_SimpleStr ing( "import time\nwhile 1:\n\tprint \"I won't
stop\"\n\ttime. sleep(1.0)\n" );
printf( "Interprete r was shutdown ... yeah \n" );
PyEval_ReleaseL ock();
}
int main(int argc, char *argv[] )
{
pthread_t thread;
PyThreadState *tstate;
Py_Initialize() ;
PyEval_InitThre ads();
tstate = PyEval_SaveThre ad();
pthread_create( &thread, (pthread_attr_t *)NULL, mythread, NULL );
sleep(3);
/* Make that pesky script die */
printf( "die ... \n" );
PyEval_AcquireL ock();
PyThreadState_C lear(gtstate);
//PyThreadState_D elete(gtstate); // Fatal Python error:
Py_EndInterpret er: not the last thread
PyThreadState_S wap(gtstate);
Py_EndInterpret er(gtstate);
PyEval_ReleaseL ock();
sleep(3); // Segmentation fault
PyEval_AcquireT hread(tstate);
Py_Finalize();
return(0);
}