pluszerodadada 2022-09-04 17:04 采纳率: 37.5%
浏览 24
已结题

c++调用python时出现错误

c++调用python时出现了以下错误:
Exception ignored in: <module 'threading' from '/usr/lib64/python3.6/threading.py'>
Traceback (most recent call last):
File "/usr/lib64/python3.6/threading.py", line 1289, in _shutdown
assert tlock.locked()

问题相关代码,请勿粘贴截图

以下是我的c++代码

void train()
{
         Py_Initialize();

        // 执行python脚本命令
        //PyRun_SimpleString("print('hello world')\n");
        PyRun_SimpleString("import sys");
        PyRun_SimpleString("sys.path.append('/usr/local/python3/lib/python3.6/site-packages')");
        PyRun_SimpleString("sys.path.append('/root/new_platform/cpp_DL/')"   
        PyObject* pModule = PyImport_ImportModule("cpython1");
        if(pModule == NULL){
            PyErr_Print();
            cout << "module not found" << endl;
        }
        PyObject* pFunc = PyObject_GetAttrString(pModule, "hello");
        if(!pFunc || !PyCallable_Check(pFunc)){
            cout << "function not found" << endl;
        }
        PyEval_CallObject(pFunc, nullptr);
        PyGILState_Release(state);
        Py_Finalize();
}

调用的python代码cpython

import argparse
def hello():
   print("parser")    
   parser = argparse.ArgumentParser(description='###train###')
   print("parser")


运行结果及报错内容

报错:
Exception ignored in: <module 'threading' from '/usr/lib64/python3.6/threading.py'>
Traceback (most recent call last):
File "/usr/lib64/python3.6/threading.py", line 1289, in _shutdown
assert tlock.locked()
希望有人能给予我解决方法

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-04 19:01
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月3日
  • 已采纳回答 11月25日
  • 创建了问题 9月4日