从C++(或C)回调中调用Python方法

18

我正在尝试从C++中调用Python类中的方法。这是一个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);

我已经尽力阐述清楚了。如果有任何问题,请回复评论。 - Chaitanya
你检查了PyObject_GetAttrString返回的是否是可用的内容吗?也许查找失败了,可能模块没有正确初始化? - djf
@djf argscallback 中被正确初始化了。这不是问题。pFunc 是全局存在的。我怀疑这一点,并在 callback 中初始化了 pFunc。但问题仍然存在。 - Chaitanya
@djf 在回调函数中初始化pFunc时出现了问题。问题是在从moduleString中加载module时发生的。在这行代码中PyImport_Import(modString); - Chaitanya
显示剩余2条评论
3个回答

35

如果您从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);
}

此外,在您的扩展模块的初始化函数中,您应该执行以下操作以确保线程正确初始化:
// 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时,可能会发生崩溃和奇怪的行为。请参阅Non-Python Created Threads以了解更多详细信息。

1
非常感谢。 :) 那就是确切的问题。 - Chaitanya
非常感谢 - 我没有考虑到我可能在错误的线程中。 - eddiewould

2

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();
}

2

这并不完全回答了你的问题,但是你可以使用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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接