实际上是python后台程序经常使用方法:
C开发完成底层的功能,python直接把C当作python模块进行调用。html
须要作两个工做:python
python能调用C语言的函数;ide
python经过调用C函数,并注册python的回调函数,C代码经过python回调函数告诉Python当前实时进度和状态;函数
1,python如何调用C语言ui
主要就是应用ctypes这个模块,too simple too naive。
python代码是这样滴:.net
from ctypes import *
dynamicLibString = './libcheckVideoFile.so'
mylib = cdll.LoadLibrary(dynamicLibString)
ulHandle = mylib.VideoAnalyzeInit(videoFilename)
if ulHandle == 0:
print 'VideoAnalyzeInit error.'
print ''
mylib.EnableBlackDetected(ulHandle)
mylib.EnablePureColorDetected(ulHandle)
mylib.EnableFrozenDetected(ulHandle)
mylib.EnableMuteVoiceDetected(ulHandle)指针
C代码是这样滴:code
unsigned long VideoAnalyzeInit(char* szFilename)
{
VideoAnalyzeManage* pManager = new VideoAnalyzeManage(szFilename);
if(pManager)
{
int iRet = pManager->Init();
if(iRet != 0)
{
delete pManager;
return 0;
}
}
return (unsigned long)pManager;
}
void EnableBlackDetected(unsigned long ulHandle)
{
VideoAnalyzeManage* pManager = (VideoAnalyzeManage*)ulHandle;
if(pManager)
{
pManager->EnableBlackDetected();
}
}htm
就像C语言编译出来的.so库只是python的一个模块,直接调用就能够了。对象
2,python注册C语言的回调函数
其实也不难,python的函数自己也是python的对象,实现也就简单了:
python的回调函数:
def OnPyVideoAnalyzeResultCallback(ulStartTS, ulEndTS, ulDetectedType, ulParam):
fStartTS = ulStartTS/1000.0
fEndTS = ulEndTS/1000.0
outputString = ''
if ulDetectedType == ALL_BLACK_DETECTED :
outputString = videoFilename + ': All black color detected: start(' + str(fStartTS) + ') end(' + str(fEndTS) + ')'
elif ulDetectedType == SIMPLE_COLOR_DETECTED :
outputString = videoFilename + ': All pure color detected: start(' + str(fStartTS) + ') end(' + str(fEndTS) + ')'
elif ulDetectedType == FROZEN_VIDEO_DETECTED :
outputString = videoFilename + ': Frozen image detected: start(' + str(fStartTS) + ') end(' + str(fEndTS) + ')'
elif ulDetectedType == AUDIO_MUTE_DETECTED :
outputString = videoFilename + ': Mute voice detected: start(' + str(fStartTS) + ') end(' + str(fEndTS) + ')'
print outputString
WriteLog(logFilename, outputString)
def OnPyVideoStateCallback(uiProgress, uiState, ulParam):
global videoFilename
outputString = ''
if uiState == DECODE_START :
outputString = '\r\n' + videoFilename + ': video analyze is starting......'
WriteLog(logFilename, outputString)
elif uiState == DECODE_RUNNING :
outputString = videoFilename + ': video analyze is running, progress: ' + str(uiProgress) + '%'
elif uiState == DECODE_END :
outputString = videoFilename + ': video analyze is ended'
WriteLog(logFilename, outputString)
elif uiState == DECODE_ERROR :
outputString = videoFilename + ': video analyze is error'
WriteLog(logFilename, outputString)
print outputString
python 两个回调函数:OnPyVideoAnalyzeResultCallback和OnPyVideoStateCallback。
如何把这两个python函数注册成C代码的回调函数呢?
python部分是这样注册滴:
CMPRESULTFUNC = CFUNCTYPE(None, c_ulong, c_ulong, c_ulong, c_ulong)
CMPSTATEFUNC = CFUNCTYPE(None, c_ulong, c_ulong, c_ulong)
iRet = mylib.VideoAnalyzeStart(ulHandle, CMPRESULTFUNC(OnPyVideoAnalyzeResultCallback), CMPSTATEFUNC(OnPyVideoStateCallback))
应用这个来设置:CFUNCTYPE
第一个参数是python回调函数的返回值,若是没有就是None。
第二个及其之后的就是python回调函数的参数类型了。
CMPRESULTFUNC = CFUNCTYPE(None, c_ulong, c_ulong, c_ulong, c_ulong)//建立一个c函数类型的对象工厂,该函数返回值为None,有三个入参,都为unsigned long。
CMPRESULTFUNC(OnPyVideoAnalyzeResultCallback)根据Python可调用对象生成函数。
mylib.VideoAnalyzeStart(ulHandle, CMPRESULTFUNC(OnPyVideoAnalyzeResultCallback), CMPSTATEFUNC(OnPyVideoStateCallback))//设置回调函数
C部分是这样的:
int VideoAnalyzeStart(unsigned long ulHandle, AnalyzeDetectedCallback resultCallback, AnalyzeStateCallback stateCallback)
{
VideoAnalyzeManage* pManager = (VideoAnalyzeManage*)ulHandle;
if(pManager)
{
pManager->SetAnalyzeResultCallback(resultCallback, 0);
pManager->SetStateNotifyCallback(stateCallback, 0);
int iRet = pManager->Start();
return iRet;
}
return -1;
}
C部分不用管。
可是如何肯定python函数参数与C函数参数的对应关系呢?
python函数参数与C函数参数的对应表(其实也能够叫ctypes类型表):
一个大坑:须要注意CMPRESULTFUNC(OnPyVideoAnalyzeResultCallback)这个指针函数是有本身的生存空间的,若是生存空间已过,会被释放,C代码再回调的时候,就会使用一个过时指针。
这里建议使用一个全局的python指针。
CMPRESULTFUNC = CFUNCTYPE(c_int, c_ulong, c_ulong, c_ulong, c_ulong)
CMPSTATEFUNC = CFUNCTYPE(c_int, c_ulong, c_ulong, c_ulong)
pResutFunc = CMPRESULTFUNC(OnPyVideoAnalyzeResultCallback)
pStateFunc = CMPSTATEFUNC(OnPyVideoStateCallback)
def main():
global pResutFunc
global pStateFunc
....
iRet = mylib.VideoAnalyzeStart(ulHandle, pResutFunc, pStateFunc)
见官网的解释:https://docs.python.org/3/library/ctypes.html#ctypes.c_long
Note
Make sure you keep references to CFUNCTYPE() objects as long as they are used from C code. ctypes doesn’t, and if you don’t, they may be garbage collected, crashing your program when a callback is made.
Also, note that if the callback function is called in a thread created outside of Python’s control (e.g. by the foreign code that calls the callback), ctypes creates a new dummy Python thread on every invocation. This behavior is correct for most purposes, but it means that values stored with threading.local will not survive across different callbacks, even when those calls are made from the same C thread. --------------------- 做者:IT荒野猎人 来源:CSDN 原文:https://blog.csdn.net/sweibd/article/details/52679213 版权声明:本文为博主原创文章,转载请附上博文连接!