linux下c语言调用python方法问题-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

linux下c语言调用python方法问题

a123456678 2016-06-13 14:57:45 2654

linux下c语言调用python接口,我有二个问题:

1、网上查资料都是c语言调用python函数的方法,有没有可以调用python类的?

2、调用过程中,c语言有设置超时的机制吗?比如我调用python里的一个方法,想对其设置超过一定时间返回。

Linux C语言 Python
分享到
取消 提交回答
全部回答(1)
  • a123456678
    2019-07-17 19:36:04
    //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
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题
推荐课程