Py_initialize / Py_Finalize与numpy的两次工作 [英] Py_initialize / Py_Finalize not working twice with numpy

查看:1975
本文介绍了Py_initialize / Py_Finalize与numpy的两次工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下code的第二个电话,我的应用程序段错误,所以我想我失去了一些东西:

On the second call of the following code, my app segfault, so I guess I am missing something :

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的错误,还是我失去了一些关于进口?

The comp_macbeth.py is importing numpy. If I remove the numpy import, everything is fine. Is it a numpy bug, or am I missing something about imports ?

推荐答案

Py_Finalize文档

某些扩展可能无法正常工作,如果它们的初始化程序被调用一次以上;如果应用程序调用Py_Initialize()和Py_Finalize()这可能发生不止一次。

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的就是其中的一个。另请参见从numpy的,讨论此消息

Apparently Numpy is one of those. See also this message from Numpy-discussion.

调用 Py_Initialize()只有一次,清理在出口,是要走的路。 (和它的应该会更快呢!)

Calling Py_Initialize() only once, and cleaning up at exit, is the way to go. (And it's should be faster, too!)

这篇关于Py_initialize / Py_Finalize与numpy的两次工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆