Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 调用Py_Finalize返回assertation错误_Python_Multithreading_Qt_Qthread - Fatal编程技术网

Python 调用Py_Finalize返回assertation错误

Python 调用Py_Finalize返回assertation错误,python,multithreading,qt,qthread,Python,Multithreading,Qt,Qthread,我尝试调用C++的python代码,我用的是下面的代码> qTox类.< /P> myclassName::myclassName() { Py_Initialize(); } myclassName::~myclassName() { Py_Finalize(); } void myclassName::cpp_wrapper(string out1, string out2){ ThreadState = PyEval_SaveThread(); GIL

我尝试调用C++的python代码,我用的是下面的代码> qTox类.< /P>
myclassName::myclassName() 
{
    Py_Initialize();
}
myclassName::~myclassName()
{
    Py_Finalize();
}

void myclassName::cpp_wrapper(string out1, string out2){

    ThreadState = PyEval_SaveThread();
    GILState = PyGILState_Ensure();

    Py_DECREF(PyImport_ImportModule("threading"));

    PyObject *moduleMain = PyImport_ImportModule("__main__");
    PyRun_SimpleString(
        "def wrapper(arg1, arg2) :          \n"\
        "   import sklearn                  \n"\
        "   print(arg1, arg2)               \n"\
        );
    PyObject *func = PyObject_GetAttrString(moduleMain, "wrapper");
    PyObject *args = PyTuple_Pack(2, PyUnicode_FromString(out1.c_str()), PyUnicode_FromString(out1.c_str()));

    Py_DECREF(moduleMain);
    Py_DECREF(func);
    Py_DECREF(args);

    PyGILState_Release(GILState);
    PyEval_RestoreThread(ThreadState);
}
void myclassName::run()
{
    algorithm_wrapper("Hello1", "Hello2");
    //here a sginal is emmited to the main thread function to delete myclassName* item.
}

int main(){
    myclassName* item = new myclassName();
    item->run();
}
执行很好(感谢上一篇文章中的@Thomas),但是当调用
Py\u Finalize
时,返回以下错误。执行python代码时,会调用
Py_Finalize
,并向主线程中的插槽发出信号以删除类对象。我还尝试在主线程中初始化和完成python(再次发送信号),但返回了相同的错误

Exception ignored in: <module 'threading' from 'C:\\Users\\username\\AppData\\Local\\Continuum\\Anaconda3\\Lib\\threading.py'>
Traceback (most recent call last):
  File "C:\Users\username\AppData\Local\Continuum\Anaconda3\Lib\threading.py", line 1289, in _shutdown
    assert tlock.locked()
.cpp文件


正如Python官方网站关于Py_Finalize中提到的:

“错误和警告:模块和模块中对象的销毁是按随机顺序进行的;这可能会导致析构函数(del()方法)在依赖于其他对象(甚至函数)时失败。”或模块。Python加载的动态加载的扩展模块不会被卸载。Python解释器分配的少量内存可能不会被释放(如果发现泄漏,请报告)。对象之间循环引用中占用的内存不会被释放。扩展模块分配的某些内存可能不会被释放。如果多次调用其初始化例程,则某些扩展可能无法正常工作;如果应用程序多次调用Py_Initialize()和Py_Finalize(),则可能会发生这种情况。”


因此,在调用Py_finalize之前,您需要手动完成ad dispose资源和流程的最终确定

您是否能够解决此问题?怎么做?@AbidRahmanK我在我的帖子中根据我使用的代码进行了编辑。老实说,我记不太清楚了。我希望这有帮助。
class myclassName : public QThread
{
    Q_OBJECT
public:
    myclassName();
    ~myclassName();

protected:
    void run();

private:
    QMutex mutex_Python;

};
myclassName::myclassName():
mutex_Python(QMutex::Recursive)
{
    Py_Initialize();
}
myclassName::~myclassName()
{
    Py_Finalize();
}

void myclassName::cpp_wrapper(string out1, string out2){

    //ThreadState = PyEval_SaveThread();
    //GILState = PyGILState_Ensure();
    mutex_Python.lock();
    Py_DECREF(PyImport_ImportModule("threading"));

    PyObject *moduleMain = PyImport_ImportModule("__main__");
    PyRun_SimpleString(
        "def wrapper(arg1, arg2) :          \n"\
        "   import sklearn                  \n"\
        "   print(arg1, arg2)               \n"\
        );
    PyObject *func = PyObject_GetAttrString(moduleMain, "wrapper");
    PyObject *args = PyTuple_Pack(2, PyUnicode_FromString(out1.c_str()), PyUnicode_FromString(out1.c_str()));

    Py_DECREF(moduleMain);
    Py_DECREF(func);
    Py_DECREF(args);
    mutex_Python.unlock();
    //PyGILState_Release(GILState);
    //PyEval_RestoreThread(ThreadState);
}
void myclassName::run()
{
    algorithm_wrapper("Hello1", "Hello2");
    //here a sginal is emmited to the main thread function to delete myclassName* item.
}

int main(){
    myclassName* item = new myclassName();
    item->run();
}