Cython封装Callback函数
1 说明:
回调函数,在C语言里是经常要用到的,但是,在Python里封装一个C的回调函数并没有想象的那么简单,本文讲解如何用Cython快速的封装C里的回调函数
2 不多说,先上代码:
cdef extern from "pthread.h":
ctypedef void * pthread_t
ctypedef structpthread_attr_t:
pass
int pthread_create (pthread_t*__newthread, \
pthread_attr_t *__attr, \
void *(*__start_routine) (void *) except*, \
void *__arg)
int pthread_join (pthread_t__th, void **__thread_return)
cdef extern from "Python.h":
ctypedef enum PyGILState_STATE:
pass
ctypedef enum PyThreadState:
pass
PyGILState_STATE PyGILState_Ensure()
void PyGILState_Release(PyGILState_STATE)
PyThreadState *PyEval_SaveThread()
void PyEval_RestoreThread(PyThreadState *)
void PyEval_InitThreads()
void Py_INCREF(object obj)
void Py_DECREF(object obj)
def Callback(obj):
if hasattr(obj,'run'):
obj.run()
cdef void * start (void * param) except*:
cdef PyGILState_STATE state
cdef object obj =