简体   繁体   中英

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. 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.

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:

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

I spent 3 hours on this problem. 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."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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