invoking interpeter in another interpeter

A

Anand K Rayudu

Hi

I have extended/embedded python into my application, it is working
great, The documentation is too good,
I could replace existing scripting tool to python in no time.


I have one problem with invoking python interpreter from another python
interpreter.
This is how it is.

I have one C API which executes python interpreter passing the file name,
So there are chances that in that file one command it self is the API to
execute python.
It is hanging in the second invocation, could some one help.

Thanks in advance
Anand


-------------------------------------------------------------------
Python code:

First python file

from Vdb import *
# This is the invocation of another python interpeter from one
ExecutePythonInterpeter("another.py" )


The application first calls the ExecutePythonInterpter C API, which in
turn calls same API for different file,
from python,

Following is the C implementation of ExecutePythonInterpeter.

void ExecutePythonInterpeter(
char *file
)
{
FILE *fp;
char *msg ;
PyObject *exc, *val, *trb, *obj ;
PyThreadState *gtstate = NULL;

if( Py_IsInitialized() == FALSE)
{
Py_Initialize();
PyEval_InitThreads();
gtstate = PyEval_SaveThread();
}
ExecutePythonInterpeterThread(file);
if(gtstate)
{
PyEval_AcquireThread(gtstate);
Py_Finalize();
gtstate=NULL;
}
return ;
}

void ExecutePythonInterpeterThread(
char *file
)
{
FILE *fp;
char file_name[256] ;
PyThreadState *tstate ;
PyEval_AcquireLock();
tstate = Py_NewInterpreter();
initVdb();
initVdbPythonVgPoint2() ;
initVdbPythonVgPoint3();
if(tstate == NULL) {
fprintf(stderr, "Can't create an interpreter\n");
return ;
}
sprintf(file_name,"%s",file);
fp=fopen(file_name,"r");
if(!fp)
{
fprintf(stderr,"FATAL:File open for %s failed\n",file);
return ;
}
PyRun_SimpleFile(fp,file_name) ;
Py_EndInterpreter(tstate);
PyEval_ReleaseLock();
fclose(fp);
return ;
}
 

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,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top