M
Mathias Mamsch
Hi all, I got a problem with writing a python extension module which
launches a bunch of C threads (currently under windows). The module launches
a complete speech recognition engine in a new thread and whenever the
recognizer recognizes a word a python callback function should be launched.
My Problem now is, after dealing with the ThreadState documentation, that in
the python test code the interpreter does not return from a time.sleep call
.... My guess is I messed around with the ThreadStates so I post some code.
please have a look at the thread specific parts of the code, and tell me how
to solve the problem properly ...
Where do I have to look for the error, what could be the reason why sleep
does not return?
Thanks in advance, greetings
Mathias Mamsch
The Python test code first, then relevant C code :
------ python code ---------
import idll # this is my module ....
import time
def Test(s): # this function gets called from the idll C++ code ...
print "CALLBACK: ", s
idll.SetCallback(Test) # this works ...
idll.StartRecognizer(1) # this works too ....
time.sleep(0.1) # and here it sleeps forever
----------------------
---------- C++ Code of the Function which gets called from some c++ thread
of the recognizer -----
---------- It should call the python function in the "Callback" variable of
the module ---------
static int CHandleFunction ()
{
// ... some vars left out
PyThreadState* tstate, *tsave;
// Create a new State for the Call ...
tstate = PyThreadState_New(mis); // mis is saved in the init
(initidll) function
PyEval_AcquireLock(); // Lock the GIL
tsave = PyThreadState_Swap(tstate); // Swap to the new State
Func = ... // Here I get the Callback from the variable
result = PyEval_CallObject(Func, PString); // Call the callback
function ... ignore PString ...
// ... Here I DecRef Func, result and some others ...
PyThreadState_Swap(tsave); // swap back the state
PyThreadState_Delete(tstate); // delete new state
PyEval_ReleaseLock(); // and release the lock
return 0;
}
// the init function ...
PyMODINIT_FUNC initidll(void)
{
PyEval_InitThreads(); // initialize the Threads module if not done yet
....
Module = Py_InitModule("idll", SpamMethods);
mts = PyThreadState_Get(); mis = mts->interp; // Try to get the main
interpreter state ...
Py_INCREF(Py_None);
PyModule_AddObject(Module, "Callback", Py_None);
PyEval_ReleaseLock();
}
launches a bunch of C threads (currently under windows). The module launches
a complete speech recognition engine in a new thread and whenever the
recognizer recognizes a word a python callback function should be launched.
My Problem now is, after dealing with the ThreadState documentation, that in
the python test code the interpreter does not return from a time.sleep call
.... My guess is I messed around with the ThreadStates so I post some code.
please have a look at the thread specific parts of the code, and tell me how
to solve the problem properly ...
Where do I have to look for the error, what could be the reason why sleep
does not return?
Thanks in advance, greetings
Mathias Mamsch
The Python test code first, then relevant C code :
------ python code ---------
import idll # this is my module ....
import time
def Test(s): # this function gets called from the idll C++ code ...
print "CALLBACK: ", s
idll.SetCallback(Test) # this works ...
idll.StartRecognizer(1) # this works too ....
time.sleep(0.1) # and here it sleeps forever
----------------------
---------- C++ Code of the Function which gets called from some c++ thread
of the recognizer -----
---------- It should call the python function in the "Callback" variable of
the module ---------
static int CHandleFunction ()
{
// ... some vars left out
PyThreadState* tstate, *tsave;
// Create a new State for the Call ...
tstate = PyThreadState_New(mis); // mis is saved in the init
(initidll) function
PyEval_AcquireLock(); // Lock the GIL
tsave = PyThreadState_Swap(tstate); // Swap to the new State
Func = ... // Here I get the Callback from the variable
result = PyEval_CallObject(Func, PString); // Call the callback
function ... ignore PString ...
// ... Here I DecRef Func, result and some others ...
PyThreadState_Swap(tsave); // swap back the state
PyThreadState_Delete(tstate); // delete new state
PyEval_ReleaseLock(); // and release the lock
return 0;
}
// the init function ...
PyMODINIT_FUNC initidll(void)
{
PyEval_InitThreads(); // initialize the Threads module if not done yet
....
Module = Py_InitModule("idll", SpamMethods);
mts = PyThreadState_Get(); mis = mts->interp; // Try to get the main
interpreter state ...
Py_INCREF(Py_None);
PyModule_AddObject(Module, "Callback", Py_None);
PyEval_ReleaseLock();
}