调用Py_Finalize returns断言错误
Calling Py_Finalize returns assertation error
我正在尝试从 C++ 调用 python 代码,我正在使用以下 QThread
class.
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();
}
执行没问题(感谢之前post中的@Thomas)但是当调用Py_Finalize
时返回以下错误。 Py_Finalize
在执行 python 代码时调用,并向主线程中的槽发出信号以删除 class 对象。我还尝试在主线程中初始化和完成 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()
能否请您提供一些帮助?
编辑: 在@Abid Rahman K 的请求之后。
.h file
class myclassName : public QThread
{
Q_OBJECT
public:
myclassName();
~myclassName();
protected:
void run();
private:
QMutex mutex_Python;
};
.cpp file
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();
}
如Python官方网站所述Py_Finalize:
"Bugs and caveats: The destruction of modules and objects in modules is done in random order; this may cause destructors (del() methods) to fail when they depend on other objects (even functions) or modules. Dynamically loaded extension modules loaded by Python are not unloaded. Small amounts of memory allocated by the Python interpreter may not be freed (if you find a leak, please report it). Memory tied up in circular references between objects is not freed. Some memory allocated by extension modules may not be freed. Some extensions may not work properly if their initialization routine is called more than once; this can happen if an application calls Py_Initialize() and Py_Finalize() more than once."
因此您在调用 Py_Finalize
之前手动完成广告处置资源和进程
我正在尝试从 C++ 调用 python 代码,我正在使用以下 QThread
class.
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();
}
执行没问题(感谢之前post中的@Thomas)但是当调用Py_Finalize
时返回以下错误。 Py_Finalize
在执行 python 代码时调用,并向主线程中的槽发出信号以删除 class 对象。我还尝试在主线程中初始化和完成 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()
能否请您提供一些帮助?
编辑: 在@Abid Rahman K 的请求之后。
.h file
class myclassName : public QThread
{
Q_OBJECT
public:
myclassName();
~myclassName();
protected:
void run();
private:
QMutex mutex_Python;
};
.cpp file
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();
}
如Python官方网站所述Py_Finalize:
"Bugs and caveats: The destruction of modules and objects in modules is done in random order; this may cause destructors (del() methods) to fail when they depend on other objects (even functions) or modules. Dynamically loaded extension modules loaded by Python are not unloaded. Small amounts of memory allocated by the Python interpreter may not be freed (if you find a leak, please report it). Memory tied up in circular references between objects is not freed. Some memory allocated by extension modules may not be freed. Some extensions may not work properly if their initialization routine is called more than once; this can happen if an application calls Py_Initialize() and Py_Finalize() more than once."
因此您在调用 Py_Finalize
之前手动完成广告处置资源和进程