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_Import Module blocks until the myModule script has run
// through...
PyObject* module = PyImport_Import Module("myModul e");
PyObject* function = PyObject_GetAtt rString(module, "GetList");
while(1) {
PyObject* pyList = PyObject_CallFu nction(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.a cquire()
tmpList = pyList
orderListLock.r elease()
return tmpList
def keepFillingList Thread():
global pyList
while 1:
orderListLock.a cquire()
pyList.append(s omestuff)
orderListLock.r elease()
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_ne w_thread(keepFi llingListThread , ())
####
Has anyone any ideas?
thanks,
nik
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_Import Module blocks until the myModule script has run
// through...
PyObject* module = PyImport_Import Module("myModul e");
PyObject* function = PyObject_GetAtt rString(module, "GetList");
while(1) {
PyObject* pyList = PyObject_CallFu nction(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.a cquire()
tmpList = pyList
orderListLock.r elease()
return tmpList
def keepFillingList Thread():
global pyList
while 1:
orderListLock.a cquire()
pyList.append(s omestuff)
orderListLock.r elease()
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_ne w_thread(keepFi llingListThread , ())
####
Has anyone any ideas?
thanks,
nik
Comment