嵌入扩展python会占用所有内存

嵌入扩展python会占用所有内存,python,c++,mingw-w64,Python,C++,Mingw W64,在跟随蟒蛇的时候,我想出了下面的代码 #include <boost/filesystem.hpp> #include <Python.h> static PyObject * spam_system(PyObject *self, PyObject *args) { const char *command; int sts; if (!PyArg_ParseTuple(args, "s", &command)) retur

在跟随蟒蛇的时候,我想出了下面的代码

#include <boost/filesystem.hpp>
#include <Python.h>
static PyObject *
spam_system(PyObject *self, PyObject *args) {
    const char *command;
    int sts;
    if (!PyArg_ParseTuple(args, "s", &command))
        return NULL;
    sts = system(command);
    return PyLong_FromLong(sts);
}
static char SpamModuleName[] = "spam\000";
int main(int argc, char const *argv[]) {
    Py_SetPath((
        boost::filesystem::canonical("./python_lib.zip").wstring()
    ).c_str());
    PyImport_AppendInittab(SpamModuleName,[](){
        static PyMethodDef SpamMethods[] = {
            {"system", spam_system, METH_VARARGS, "Execute a shell command."},
            {NULL, NULL, 0, NULL}
        };
        static struct PyModuleDef spammodule = {
            PyModuleDef_HEAD_INIT,
            SpamModuleName,
            NULL,
            -1,
            SpamMethods,
            NULL, NULL, NULL, NULL
        };
        return PyModule_Create(&spammodule);
    });
    Py_Initialize();
    PyRun_SimpleString(
        "import spam\n"
        "status = spam.system(\"ls -l\")\n"
    );
    Py_Finalize();
    return 0;
}
#包括
#包括
静态PyObject*
垃圾邮件_系统(PyObject*self,PyObject*args){
const char*命令;
int sts;
if(!PyArg_ParseTuple(args,“s”和命令))
返回NULL;
sts=系统(命令);
从长(sts)返回PyLong_;
}
静态char SpamModuleName[]=“spam\000”;
int main(int argc,char const*argv[]{
Py_设置路径((
boost::filesystem::canonical(“./python_lib.zip”).wstring()
).c_str());
PyImport_AppendInittab(SpamModuleName,[](){
静态PyMethodDef SpamMethods[]={
{“system”,spam_system,METH_VARARGS,“执行shell命令。”},
{NULL,NULL,0,NULL}
};
静态结构PyModuleDef spammodule={
PyModuleDef_HEAD_INIT,
垃圾邮件模块名,
无效的
-1,
垃圾方法,
空,空,空,空
};
返回PyModule_Create(&spammodule);
});
Py_初始化();
皮伦·欧·斯普勒斯特林(
“导入垃圾邮件\n”
“状态=垃圾邮件。系统(\'ls-l\”)\n”
);
Py_Finalize();
返回0;
}
代码编译得很好(使用
g++-std=c++11 main.cpp-lpython33.64-lboost_filesystem-lboost_system-s
我使用Stephan T.Lavavej的x64),但在运行我的程序时分配了大约4G的ram,并且在
PyRun_简单限制(“导入垃圾邮件”)中有100%的cpu使用率
并且经常与python
MemoryError发生崩溃

PyImport\u导入模块(SpamModuleName)也会使程序崩溃,也是在分配了大量内存之后(事实上,我从未成功运行过此函数)

如果我结束所有其他程序并释放尽可能多的ram,程序运行良好并产生预期的输出,但资源消耗使其无法销售。我做错了什么/是什么让python使用了这么多资源


编辑在讨论了mingw-w64 irc之后,我找到了它,并将该解决方案作为一个答案发布,以防其他人发现自己在我的位置上

多亏了用户的广泛帮助
alexey
ktietz
我被指出,使用64位VC构建的python.dll在导入构建的x64二进制文件时遇到问题海湾合作委员会。解决方案是自己构建lib,同时修补它以编译unter MINGWx64


您可以找到或找到相关的。

此开放bug讨论了mingw和python扩展的问题