Py_initialize/Py_Finalize未使用numpy两次

2024-09-29 06:24:14 发布

您现在位置:Python中文网/ 问答频道 /正文

在以下代码的第二次调用中,我的应用程序SEGFULT,因此我想我遗漏了一些东西:

Py_Initialize();
pName = PyString_FromString("comp_macbeth");
pModule = PyImport_Import(pName);
Py_DECREF(pName);

if(pModule == NULL) {
    PyErr_Print();
    Py_Finalize();
    return;
}

pFunc = PyObject_GetAttrString(pModule, "compute");
/* pFunc is a new reference */

if (!pFunc || !PyCallable_Check(pFunc) ) {
    PyErr_Print();
    Py_Finalize();
    return;
}

Py_Finalize();

comp_macbeth.py正在导入numpy。如果我删除numpy导入,一切都很好。这是一个numpy bug,还是我遗漏了一些关于导入的内容


Tags: 代码pynumpy应用程序returnifprintcomp
2条回答

Py_Finalize docs开始:

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.

显然,努比就是其中之一。另见Numpy讨论中的this message

只调用Py_Initialize()一次,然后清理at exit,这是一种方法。(而且速度也应该更快!)

在我的模块初始化部分有这个,但是URL不再存在了。如果有帮助:

// http://numpy.scipy.org/numpydoc/numpy-13.html mentions this must be done in module init, otherwise we will crash
import_array();

相关问题 更多 >