C++ app calling python script repeatedly

nik my.news.groups at noos.fr
Mon Aug 16 05:10:49 EDT 2004


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



More information about the Python-list mailing list