簡體   English   中英

從C ++(或C)回調中調用python方法

[英]Calling python method from C++ (or C) callback

我試圖從C ++調用python類中的方法。 調用它的C ++方法是C ++回調。

在我嘗試調用python方法時,在這種方法中,它給出了segmentation fault

我已經在一個全局變量中保存了一個python函數實例

// (pFunc is global variable of type PyObject*)
pFunc = PyDict_GetItemString(pDict, "PlxMsgWrapper");

其中PlxMsgWrapper是一個python方法,將在回調中使用。

在回調中,參數創建為

PyObject* args = PyTuple_Pack(2, PyString_FromString(header.c_str()),
                                 PyString_FromString(payload.c_str()));

在創建時

PyObject * pInstance = PyObject_CallObject(pFunc, args);

在這一行中它給出了分段錯誤。 在此之后,實際的python方法被稱為

PyObject* recv_msg_func = PyObject_GetAttrString(module, (char *)"recvCallback");
args = PyTuple_Pack(1, pInstance);
PyObject_CallObject(recv_msg_func, args);

如果從C / C ++回調調用Python函數,則需要執行一些操作。 首先,當你保存你的python函數對象時,你需要增加引用計數:

Py_INCREF(pFunc)

否則,Python不知道您持有對象引用,它可能會垃圾收集它,當您嘗試從回調中使用它時會導致分段錯誤。

接下來你需要關心的是調用C / C ++回調時正在運行的線程。 如果你從另一個非Python創建的線程(即在套接字上接收數據的C / C ++線程)回調,那么在調用任何Python API函數之前,你必須獲得Python的全局解釋器鎖(GIL)。 否則你的程序的行為是不確定的。 要獲得GIL,您需要:

void callback() {
    PyGILState_STATE gstate;
    gstate = PyGILState_Ensure();

    // Get args, etc.

    // Call your Python function object
    PyObject * pInstance = PyObject_CallObject(pFunc, args);

    // Do any other needed Python API operations

    // Release the thread. No Python API allowed beyond this point.
    PyGILState_Release(gstate);
}

此外,在擴展模塊的init函數中,您應該執行以下操作以確保正確初始化線程:

// Make sure the GIL has been created since we need to acquire it in our
// callback to safely call into the python application.
if (! PyEval_ThreadsInitialized()) {
    PyEval_InitThreads();
}

否則,當您嘗試從非Python線程獲取GIL時,可能會發生崩潰和奇怪的行為。

有關此內容的更多詳細信息,請參閱非Python創建的線程

Python應該在運行它的目錄中查找模塊,但是,如果您認為問題是python沒有找到您的文件,您可以將計算機上的任意目錄添加到程序中的模塊搜索路徑:

// Initialize the Python Interpreter
Py_Initialize();

// The following two lines to the trick:
// add path to your module to python's search paths
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append(\"/path/to/python/module/here\")");

// Build the name object
pName = PyString_FromString("your_module");

// Load the module object
pModule = PyImport_Import(pName);

// pDict is a borrowed reference 
pDict = PyModule_GetDict(pModule);

// pFunc is also a borrowed reference 
pFunc = PyDict_GetItemString(pDict, "PlxMsgWrapper");

pArgs = ... 

if (PyCallable_Check(pFunc)) 
{
   PyObject_CallObject(pFunc, pArgs);
} else {
   PyErr_Print();
}

這並不能完全回答您的問題,但您可以大大簡化代碼並避免使用Boost :: Python引用計數問題。

#include "boost/python.hpp"

using namespace boost::python;

int main()
{
  Py_Initialize();

  object pyFunPlxMsgWrapper = import("your_module").attr("PlxMsgWrapper");
  pyFunPlxMsgWrapper(2, "string", "data");
  return 0;
}

暫無
暫無

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

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