diff --git a/execute_py.c b/execute_py.c index 286a578..8298009 100644 --- a/execute_py.c +++ b/execute_py.c @@ -9,41 +9,31 @@ #define AS_STRING2(X) #X // loads the function F from the shared lib -#define LOAD(F, R, ...) \ - R (*F)(__VA_ARGS__); \ - *(void**)(&F) = dlsym(libpython_handle, AS_STRING(F)); \ - if ((error = dlerror()) != NULL) { \ - fprintf(stderr, "%s\n", error); \ - return 1; \ +#define LOAD(F, R, ...) \ + R (*F)(__VA_ARGS__); \ + *(void**)(&F) = dlsym(handle, AS_STRING(F)); \ + if ((error = dlerror()) != NULL) { \ + fprintf(stderr, "%s\n", error); \ + return 1; \ } -static void* libpython_handle = NULL; - -//--------------------------------- - -int init_python_interpreter(const char* python_so) { +int execute_python_file(const char* python_so, const char* filename) { + static void* handle = NULL; // will be not NULL iif Python already runs char* error; - libpython_handle = dlopen(python_so, RTLD_GLOBAL | RTLD_LAZY); - if (!libpython_handle) { - fprintf(stderr, "Can not find Python !\n%s\n", dlerror()); - return 1; - } - dlerror(); // Clear any existing error - LOAD(Py_Initialize, void, ); // loads the functions that we will need - (*Py_Initialize)(); // initialize the interpreter - return 0; -} -//--------------------------------- - -int execute_python_file(const char* filename) { - char* error; - if (!libpython_handle) { - fprintf(stderr, "Python is not initialized. You forget to call init_python_interpreter !\n"); - return 1; + // opens the python shared lib and init the interpreter + if (!handle) { + handle = dlopen(python_so, RTLD_GLOBAL | RTLD_LAZY); + if (!handle) { + fprintf(stderr, "Can not find Python !\n%s\n", dlerror()); + return 1; + } + dlerror(); // Clear any existing error + LOAD(Py_Initialize, void, ); // loads the functions that we will need + (*Py_Initialize)(); // initialize the interpreter } - // check Python is running + // check Python is running if handle is not NULL LOAD(Py_IsInitialized, int, ); if (!(*Py_IsInitialized)()) { fprintf(stderr, "Python Interpreter failed to initialize"); @@ -60,20 +50,13 @@ int execute_python_file(const char* filename) { LOAD(PyRun_SimpleFile, int, FILE*, const char*); (*PyRun_SimpleFile)(file, filename); - return 0; -} - -//--------------------------------- - -int close_python_interpreter() { - char* error; - + // We never close the interpreter nor the handle // close the interpreter - LOAD(Py_Finalize, void, ); - (*Py_Finalize)(); - + // LOAD(Py_Finalize, void, ); + // (*Py_Finalize)(); + // Never close ! // close the shared lib - dlclose(libpython_handle); + // dlclose(handle); return 0; } diff --git a/execute_py.h b/execute_py.h index d28d147..cc5bb6f 100644 --- a/execute_py.h +++ b/execute_py.h @@ -1,9 +1,8 @@ -// starts the interpreter -int init_python_interpreter(const char* python_so); -// executes code in the Interpreter -int execute_python_file(const char* filename); - -// call only ONCE at the very end ! -int close_python_interpreter(); +// Launch the intepreter, runs filename into it and close it +// +// python_so : location of libpython.xx.so or libpython.xx.dylib on OS X +// filename : script to be run in the interpreter +// +int execute_python_file(const char *python_so, const char *filename); diff --git a/main.c b/main.c index acd2de4..5b3301c 100644 --- a/main.c +++ b/main.c @@ -24,13 +24,11 @@ int main(int argc, char** argv) { printf("Hello world! I'm process %i out of %i processes\n", my_id, num_procs); // launch python - init_python_interpreter(argv[1]); - - execute_python_file(argv[2]); + execute_python_file(argv[1], argv[2]); printf("Between 2 calls from C : I'm process %i out of %i processes\n", my_id, num_procs); - execute_python_file(argv[2]); + execute_python_file(argv[1], argv[2]); // check it has not been finalized int final; @@ -48,6 +46,5 @@ int main(int argc, char** argv) { ierr = MPI_Comm_size(MPI_COMM_WORLD, &num_procs); printf("Hello again ! I'm process %i out of %i processes\n", my_id, num_procs); - close_python_interpreter(); MPI_Finalize(); }