1

我能够从本机到 Cython 获得正常的回调。

但是,如何在 cython 中将成员函数设置为回调。

我的sampleCallback.h文件是:

 namespace mango {
 class sampleCalls { 
 public:
     typedef void (*Callback)(char *name);

     sampleCalls(Callback method, char *name);
     void execute();
 private:
     Callback _method;
     char *_key;
 }; }

我的sampleCallback.cpp文件包含简单的实现:

 namespace mango {
        sampleCalls::sampleCalls(Callback method, char *name) {
                _method = method;
                _key = name;
        };
        void sampleCalls::execute()
        {
                return _method(_key);
        };
}

现在我写了一个非常简单的 cython 包装器callback.pyx

ctypedef void (*Callback)(char *name)

cdef extern from "sampleCallback.h" namespace "mango" :
        cdef cppclass sampleCalls:
                sampleCalls(Callback method, char *name)
                void execute()

cdef void callbackCY(char* name):
        print "I AM ", name

cdef class pyCallback:
    cdef sampleCalls* sc

    def __init__(self):
            self.sc = new sampleCalls(callbackCY, "BATMAN")

    def  execute(self):
            self.sc.execute()

当我运行一个简单的 python 代码时,它可以正常工作:

import callback
cb = callback.pyCallback()
cb.execute()

这工作得很好并且输出:: I AM BATMAN

但这不是我想要的。我希望回调函数成为 cython 定义类的一部分,所以我实现了这样的类:

cdef class pyCallback:
    cdef sampleCalls* sc

    def __init__(self):
        self.sc = new sampleCalls(cb, "BATMAN")

    def  execute(self):
        self.sc.execute()

    cdef void cb(self, char* name):
        print name, " WINS"

这当然不起作用,因为回调原型不匹配。

正确的方法是什么?

我也试过 std::bind 这样的东西::

cdef place1 "std::placeholder1"
cdef bind_py "std::bind"

cdef class pyCallback:
    cdef sampleCalls* sc

    def __init__(self):
        self.sc = new sampleCalls(bind_py(self.cb, self, place1),     "BATMAN")

这也不起作用。错误 ::

Error compiling Cython file:
------------------------------------------------------------
...
cdef class pyCallback:
    cdef sampleCalls* sc

    def __init__(self):
            self.sc = new sampleCalls(bind_py(self.cb, self, place1), "BATMAN")
                                   ^
------------------------------------------------------------

callback.pyx:19:40: Cannot convert 'void (pyCallback, char *)' to Python object

Error compiling Cython file:
------------------------------------------------------------
...
cdef class pyCallback:
    cdef sampleCalls* sc

    def __init__(self):
            self.sc = new sampleCalls(bind_py(self.cb, self, place1), "BATMAN")
                              ^
------------------------------------------------------------

callback.pyx:19:35: Cannot convert Python object to 'Callback'

我怎样才能做到这一点?请帮忙。

4

0 回答 0