python可以调用c语言吗_linux下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();*/

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值