Embedded python with threads

nik my.news.groups at noos.fr
Wed Sep 15 18:05:54 CEST 2004


hi,

I have a C++ app that loads a python module and calls a function from 
it. The call is getting the contents of a list that's global in that module.

However, I'd like the python script to be also running a thread that 
fills that global list, but can't figure out how to do it. Essentially 
the code looks like;

in the C++ app;

////
Py_Initialize();

// PyImport_ImportModule blocks until the myModule script has run
// through...
PyObject* module = PyImport_ImportModule("myModule");

PyObject* function = PyObject_GetAttrString(module, "GetList");
while(1) {
	PyObject* pyList = PyObject_CallFunction(function, "");
	// use the stuff in pyList
	sleep(15);
	}
////

and in the myModule.py;

####
import thread
import time
orderListLock = thread.allocate_lock()

pyList = []

def GetList():
	global pyList
	tmpList = []
	orderListLock.acquire()
	tmpList = pyList
	orderListLock.release()
	return tmpList

def keepFillingListThread():
	global pyList
	while 1:
		orderListLock.acquire()
		pyList.append(somestuff)
		orderListLock.release()
		time.sleep(5)

# the following statement happens when the C++ app imports the module,
# but the thread only lives as long the main python thread (which is
# over straight away on my linux PC)

thread.start_new_thread(keepFillingListThread, ())

####

Has anyone any ideas?

thanks,
nik



More information about the Python-list mailing list