简体   繁体   English

在64位Windows上的嵌入式Python 3.4中添加新模块时,速度变慢并占用大量内存

[英]Slowdown and high memory uasge when adding a new module in embedded Python 3.4 on 64bit Windows

I need to have my Python program access my C functions. 我需要让我的Python程序访问我的C函数。 So I used the example from the documentation to get started. 因此,我使用文档中示例开始使用。

Unfortunately, its very slow and it takes up all of my memory. 不幸的是,它非常慢,占用了我的全部内存。

I am using Windows 7 64bit, Python 3.4 and gcc (rubenvb-4.8.0) 4.8.0. 我正在使用Windows 7 64位,Python 3.4和gcc(rubenvb-4.8.0)4.8.0。

For completeness sake, here's the code from the documentation: 为了完整起见,下面是文档中的代码:

#include <Python.h>

static int numargs=0;

static PyObject*
emb_numargs(PyObject *self, PyObject *args)
{
    if(!PyArg_ParseTuple(args, ":numargs"))
        return NULL;
    return PyLong_FromLong(numargs);
}

static PyMethodDef EmbMethods[] = {
    {"numargs", emb_numargs, METH_VARARGS,
     "Return the number of arguments received by the process."},
    {NULL, NULL, 0, NULL}
};

static PyModuleDef EmbModule = {
    PyModuleDef_HEAD_INIT, "emb", NULL, -1, EmbMethods,
    NULL, NULL, NULL, NULL
};

static PyObject*
PyInit_emb(void)
{
    return PyModule_Create(&EmbModule);
}

int main()
{
    [...]
    numargs = argc;
    PyImport_AppendInittab("emb", &PyInit_emb);

    Py_Initialize();
    pModule = PyImport_Import(pName);
    [...]
}

The python program has the following code: python程序具有以下代码:

import emb
print("Number of arguments", emb.numargs())

I spent 3 hours on this problem. 我花了3个小时解决这个问题。 I found the solution at this page . 我在此页面找到了解决方案。

Apparently, you need to have the following defined: 显然,您需要定义以下内容:

#define HAVE_SSIZE_T

The page said that "This ensures that the type Py_ssize_t becomes __int64 instead of int." 页面上说:“这确保类型Py_ssize_t变为__int64而不是int。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM