簡體   English   中英

在64位Windows上的嵌入式Python 3.4中添加新模塊時,速度變慢並占用大量內存

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

我需要讓我的Python程序訪問我的C函數。 因此,我使用文檔中示例開始使用。

不幸的是,它非常慢,占用了我的全部內存。

我正在使用Windows 7 64位,Python 3.4和gcc(rubenvb-4.8.0)4.8.0。

為了完整起見,下面是文檔中的代碼:

#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);
    [...]
}

python程序具有以下代碼:

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

我花了3個小時解決這個問題。 我在此頁面找到了解決方案。

顯然,您需要定義以下內容:

#define HAVE_SSIZE_T

頁面上說:“這確保類型Py_ssize_t變為__int64而不是int。”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM