从C调用Python ++ [英] Call Python from C++

查看:322
本文介绍了从C调用Python ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的主C ++程序中调用Python脚本的功能。 Python函数接受一个字符串作为参数,并没有返回值(确定..'无')。
它的工作原理非常清楚(从来没有想到会是那么容易。)只要函数被再次调用之前的previous通话结束,否则在 pModule = PyImport_Import访问冲突( PNAME)

有很多教程如何嵌入在C Python而反之亦然,但我没有发现任何有关问题。

  INT callPython(TCHAR *标题){
    *的PyObject PNAME,* pModule,* pFunc;
    *的PyObject pArgs,* P值;Py_Initialize();
    PNAME = PyUni code_FromString(主);
    / * Pythonfile名称* /    pModule = PyImport_Import(PNAME);
    Py_DECREF(PNAME);如果(pModule!= NULL){
        pFunc = PyObject_GetAttrString(pModulewriteLyricToFile);
        / *函数名。 pFunc是一个新的参考* /
        如果(pFunc&放大器;&放大器; PyCallable_Check(pFunc)){
            pArgs = PyTuple_New(1);    P值= PyUni code_FromWideChar(标题,-1);    如果(!P值){
        Py_DECREF(pArgs);
                Py_DECREF(pModule);
        showErrorBox(_T(P值是假));
        返回1;
            }
    PyTuple_SetItem(pArgs,0,P值);            P值= PyObject_CallObject(pFunc,pArgs);
            Py_DECREF(pArgs);            如果(P值!= NULL){
                //因为它应该工作!
                Py_DECREF(P值);
            }
            其他{
                Py_DECREF(pFunc);
                Py_DECREF(pModule);
                PyErr_Print();
        showErrorBox(_T(P值为空));
        返回1;
            }
        }
        其他{
            如果(PyErr_Occurred())PyErr_Print();
            showErrorBox(_T(pFunc空或不是调用));
        返回1;
        }
        Py_XDECREF(pFunc);
        Py_DECREF(pModule);
    }
    其他{
        PyErr_Print();
        showErrorBox(_T(pModule为空));
    返回1;
    }
    Py_Finalize();
    返回0;
}


解决方案

当你说只要函数被再次调用之前的previous通话结束,我只能假设你有多个线程调用从C ++到Python。蟒蛇不是线程安全的,所以这是要失败的!

阅读上在Python手册全球国米preTER锁(GIL)。也许下面的链接将帮助:

该GIL是维基百科上提到的:

I'm trying to call a function in a Python script from my main C++ program. The python function takes a string as the argument and returns nothing (ok.. 'None'). It works perfectly well (never thought it would be that easy..) as long as the previous call is finished before the function is called again, otherwise there is an access violation at pModule = PyImport_Import(pName).

There are a lot of tutorials how to embed python in C and vice versa but I found nothing about that problem.

int callPython(TCHAR* title){
    PyObject *pName, *pModule, *pFunc;
    PyObject *pArgs, *pValue;

Py_Initialize();
    pName = PyUnicode_FromString("Main");
    /* Name of Pythonfile */

    pModule = PyImport_Import(pName);
    Py_DECREF(pName);

if (pModule != NULL) {
        pFunc = PyObject_GetAttrString(pModule, "writeLyricToFile");
        /* function name. pFunc is a new reference */
        if (pFunc && PyCallable_Check(pFunc)) {
            pArgs = PyTuple_New(1);

    pValue = PyUnicode_FromWideChar(title, -1);

    if (!pValue) {
        Py_DECREF(pArgs);
                Py_DECREF(pModule);
        showErrorBox(_T("pValue is false"));
        return 1;
            }
    PyTuple_SetItem(pArgs, 0, pValue);

            pValue = PyObject_CallObject(pFunc, pArgs);
            Py_DECREF(pArgs);

            if (pValue != NULL) {
                //worked as it should!
                Py_DECREF(pValue);
            }
            else {
                Py_DECREF(pFunc);
                Py_DECREF(pModule);
                PyErr_Print();
        showErrorBox(_T("pValue is null"));
        return 1;
            }
        }
        else {
            if (PyErr_Occurred()) PyErr_Print();
            showErrorBox(_T("pFunc null or not callable"));
        return 1;
        }
        Py_XDECREF(pFunc);
        Py_DECREF(pModule);
    }
    else {
        PyErr_Print();
        showErrorBox(_T("pModule is null"));
    return 1;
    }
    Py_Finalize();
    return 0;
}

解决方案

When you say "as long as the previous call is finished before the function is called again", I can only assume that you have multiple threads calling from C++ into Python. The python is not thread safe, so this is going to fail!

Read up on the Global Interpreter Lock (GIL) in the Python manual. Perhaps the following links will help:

The GIL is mentioned on Wikipedia:

这篇关于从C调用Python ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆