C code embedded python callback function,and put data to python queue through callback, when i get data from queue, it's very slow.
Example:
c code like this
static int wrap_func(const int a, const unsigned char *b)
{
long ret;
PyObject *arglist;
PyObject * result = NULL;
arglist = Py_BuildValue("(s#)", b, a);
result = PyEval_CallObject(my_callback, arglist);
/* evaluate result or handle exception */
ret = PyInt_AsLong(result);
if (result == NULL)
return -1;
Py_DECREF(result);
return ret;
}
void produce_data()
{
while(1){
//produce data to buffer, len is buffer length
//call callback func
wrap_func(buffer, len);
}
}
compile this c code to so like mywrap.so, and import this so in python python code like this:
import multiprocessing
import mywarp # mywrap.so
class WorkerThread_a(threading.Thread):
def __init__(self, workQueue):
threading.Thread.__init__(self)
self.workQueue = workQueue
self.setDaemon(True)
def run(self):
while 1:
try:
recvdata = self.workQueue.get(block=False)
except Queue.Empty:
continue
#do sth use recvdata
workQueue = multiprocessing.Queue()
def callback_func(a):
if a:
workQueue.put(a)
return 0
def main():
tmp = WorkerThread_a(workQueue)
tmp.start()
mywarp.set_callback(callback_func)
mywarp.decode_audio()
main()
In python thread, i get data from queue, but i get data very slowly, but in c so, produce data and put to queue through python callback func quickly.
how can i get data from queue quickly like in pure python code.