C++ app calling python script repeatedly

N

nik

hi,

my C++ app has embedded the interpreter and calls a python script
everytime it creates a certain data structure. This allows the user to
put the structure into a database or files or whatever. For example;

// Py_Initialize(); has been called earlier

m_module = PyImport_AddModule("myModule");
// for example, this string is my data structure
PyObject* theMessage = PyString_FromString("data");
PyModule_AddObject(my_module, "Message", theMessage);

fp =fopen("theScript.py", "r");
PyRun_SimpleFile(fp, "theScript.py");

// Py_Finalize(); will be called when the C++ app exits

I've just realised that calling the script repeatedly might not be the
best thing, especially if the users script has a lot of initialisation
to do, or needs to store variables in between calls to the script.

How could I solve this? Can the user run a python program alongside my
C++ app, and the app call a method on the users program? I.e. can the
script I call be part of a global process in some way? Or, are there
other ways to go about this?

nik
 
B

Brian Quinlan

nik said:
How could I solve this? Can the user run a python program alongside my
C++ app, and the app call a method on the users program? I.e. can the
script I call be part of a global process in some way? Or, are there
other ways to go about this?

nik

You might want to think about how you would do this if the entire
application were written in Python. It sounds like "theScript.py" is a
module that provides some sort of service to the rest of your
application. So you would probably be best to import it once and then
call a function in that module every time that you need to.

Cheers,
Brian
 

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

Staff online

Members online

Forum statistics

Threads
474,296
Messages
2,571,535
Members
48,279
Latest member
RedaBruno6

Latest Threads

Top