在遵循pythonsembedding/extending tutorial时,我产生了以下代码
#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;
}
代码编译得很好(使用Stephan T.Lavavej的x64native mingw toolchain),但是在运行我的程序时分配了大约4gig的ram,并且在PyRun_SimpleString("import spam\n")
中有100%的cpu使用率(procexp screenshot),而pythons MemoryError
经常崩溃。你知道吗
PyImport_ImportModule(SpamModuleName);
在分配了大量内存之后,程序也崩溃了(事实上,我从来没有成功运行过这个函数)。你知道吗
如果我结束所有其他程序并释放尽可能多的ram,程序运行良好并产生预期的输出,但资源消耗使其无法使用。我做错了什么/是什么让python使用了那么多资源?你知道吗
编辑在讨论了mingw-w64 irc后,我得到了它的工作,并将发布解决方案作为答案,以防其他人发现自己在我的位置上
多亏了用户
alexey
和ktietz
的广泛帮助,我被指出了这个事实python.dll使用64位VC构建的系统在导入GCC构建的x64二进制文件时遇到问题。解决方案是自己构建lib,同时修补它以编译unter MINGWx64。你知道吗我们可以找到those patches here或prebuilt libraries here。你知道吗
相关问题 更多 >
编程相关推荐