从 C++(或 C)回调调用 python 方法

2022-01-03 00:00:00 python c callback python-c-api c++

我正在尝试从 C++ 调用 python 类中的方法.调用 this 的 C++ 方法是 C++ 回调.

I am trying to call methods in a python class from C++. The C++ method from which this is called is a C++ callback.

在这个方法中,当我尝试调用 python 方法时,它给出了segmentation fault.

Within this method when I am trying to call python method, it was giving segmentation fault.

我已经在一个全局变量中保存了一个 python 函数的实例,比如

I have saved an instance of python function in a global variable like

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

其中 PlxMsgWrapper 是一个 Python 方法,将在回调中使用.

where PlxMsgWrapper is a python method, which will be used in the callback.

在回调中,参数被创建为

In the callback, the arguments are created as

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

创建时

PyObject * pInstance = PyObject_CallObject(pFunc, args);

在这一行中,它给出了分段错误.在此之后,实际的python 方法被称为

In this line its giving segmentation fault. After this the actual python method is called as

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

推荐答案

如果要从 C/C++ 回调调用 Python 函数,则需要做一些事情.首先,当你保存你的 python 函数对象时,你需要增加引用计数:

There are a few things you need to do if you are invoking a Python function from a C/C++ callback. First when you save off your python function object, you need to increment the reference count with:

Py_INCREF(pFunc)

否则 Python 不知道您正在持有一个对象引用,它可能会对其进行垃圾回收,从而在您尝试从回调中使用它时导致分段错误.

Otherwise Python has no idea you are holding onto an object reference, and it may garbage collect it, resulting in a segmentation fault when you try to use it from your callback.

那么接下来您需要关心的是当您的 C/C++ 回调被调用时正在运行的线程.如果您从另一个非 Python 创建的线程(即在套接字上接收数据的 C/C++ 线程)回调,那么您必须在调用任何 Python API 之前获取 Python 的全局解释器锁 (GIL)职能.否则你的程序的行为是未定义的.要获得 GIL,您可以:

Then next thing you need to be concerned about is what thread is running when your C/C++ callback is invoked. If you are getting called back from another non-Python created thread (i.e. a C/C++ thread receiving data on a socket), then you MUST acquire Python's Global Interpreter Lock (GIL) before calling any Python API functions. Otherwise your program's behavior is undefined. To acquire the GIL you do:

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 函数中,您应该执行以下操作以确保正确初始化线程:

Also, in your extension module's init function, you should do the following to ensure that threading is properly initialized:

// 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 时,可能会发生崩溃和奇怪的行为.

Otherwise, crashes and strange behavior may ensue when you attempt to acquire the GIL from a non-Python thread.

参见 非-Python 创建线程了解更多详情.

相关文章