linux下c语言调用python接口,我有二个问题:
1、网上查资料都是c语言调用python函数的方法,有没有可以调用python类的?
2、调用过程中,c语言有设置超时的机制吗?比如我调用python里的一个方法,想对其设置超过一定时间返回。
//c语言多线程调python,必须加上红色字体,因为python本身不是线程安全的
PyObject* callPython(char* cpModelPath)
{
if(cpModelPath == NULL)
return NULL;
char modelPath[100] = "";
char modelName[21] = "";
int i;
char* cp = cpModelPath;
int len = strlen(cp);
int len1 = len;
if(len <= 0)
{
printf( "model is null, pid [%u] exit\n", (unsigned int)getpid() );
return;
}
while( *(cp + len - 1) != '/')
{
len--;
}
for(i = 0; i < len; ++i)
modelPath[i] = *(cp+i);
for(i = 0; i < len1-len; ++i)
modelName[i] = *(cp+i+len);
char modelPath1[100] = "";
strcpy(modelPath1, "sys.path.append('");
strcat(modelPath1, modelPath);
strcat(modelPath1, "')");
PyRun_SimpleString("import sys");
PyRun_SimpleString(modelPath1);
PyObject* pModule = NULL;
PyObject* pDict = NULL;
PyObject* pClass = NULL;
PyObject* pObject = NULL;
PyObject* pFunc = NULL;
//调用python中的类
pModule = PyImport_ImportModule(modelName);
if(!pModule)
{
printf( "model PyImport_ImportModule fail, pid [%u] thread [%u] exit\n",
(unsigned int)getpid(), (unsigned int)pthread_self() );
return;
}
pDict = PyModule_GetDict(pModule);
if(!pDict)
{
printf( "model PyModule_GetDict fail, pid [%u] thread [%u] exit\n",
(unsigned int)getpid(), (unsigned int)pthread_self() );
return;
}
pClass = PyObject_GetAttrString(pModule, "JobNode");//得到那个类
if(!pClass || !PyCallable_Check(pClass))
{
printf( "model PyObject_GetAttrString class fail, pid [%u] thread [%u] exit\n",
(unsigned int)getpid(), (unsigned int)pthread_self() );
return;
}
pObject = PyEval_CallObject(pClass, NULL);//生成一个对象,或者叫作实例
pFunc = PyObject_GetAttrString(pObject, "createApp");//得到该实例的成员函数
if(!pFunc || !PyCallable_Check(pFunc))
{
printf( "model PyObject_GetAttrString func fail, pid [%u] thread [%u] exit\n",
(unsigned int)getpid(), (unsigned int)pthread_self() );
return;
}
Py_DECREF(pObject);
Py_DECREF(pClass);
Py_DECREF(pDict);
Py_DECREF(pModule);
return pFunc;
}
void* func(void* arg)
{
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
PyObject* pFunc = NULL;
PyObject* pArg = NULL;
PyObject* pString = NULL;
char* cpReturn;
PyObject* p = callPython("/mnt/hgfs/D/emar_test/linux-test/marmoset/JobNode");
pFunc = p;
pArg = PyTuple_New(1);
// s 表示字符串,
// i 表示整型变量,
// f 表示浮点数,
// O 表示一个Python对象。
PyTuple_SetItem(pArg, 0, Py_BuildValue("s", "Jacky"));
pString = PyEval_CallObject(pFunc, pArg);//执行该实例的成员函数
cpReturn = PyString_AsString(pString);
printf("cpReturn=%s\n",cpReturn);
Py_DECREF(pString);
Py_DECREF(pArg);
Py_DECREF(pFunc);
PyGILState_Release(gstate);
}
void main()
{
int num = 1;
Py_Initialize();
if(!Py_IsInitialized())
{
return;
}
PyEval_InitThreads();
PyEval_ReleaseThread(PyThreadState_Get());
pthread_t pid1,pid2;
pthread_create(&pid1, NULL, func, NULL);
pthread_create(&pid2, NULL, func, NULL);
pthread_join(pid1, NULL);
pthread_join(pid2, NULL);
PyGILState_Ensure();
Py_Finalize();*/
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
你好,我是AI助理
可以解答问题、推荐解决方案等
评论
全部评论 (0)