SWIG在新线程中运行python回调

2024-09-30 07:28:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚开始使用SWIG,我遇到了一些困难,尝试发送一个指向函数的指针并运行它,特别是在一个新线程中运行它。在

我看到了一些关于函数指针的问题,但是我不知道它的使用指导原则,我确信在一个新线程中运行它也会带来更多的问题。在

这是我的代码:

#.h file
#include <vector>
#include <memory>
class Die
{
public:
    Die();
    Die(int a);
    ~Die();
    int foo(int a) ;
    std::vector<int> foo2(int b) ;
    std::shared_ptr<Die> getDie(int a);
    void funcTest(void* (*foo5)(void*), int a);
    int myVar;
};

#.cpp file

^{pr2}$

我的.i文件

/* File: example.i */
%module example
%include "stdint.i"
%include "std_vector.i"
%include <std_shared_ptr.i>
%shared_ptr(Die)
%{
#include "example.h"

%}
%include "example.h"

/*void Die::funcTest(void* (*foo5)(void*), int a);*/
%template(DieVector) std::vector<Die*>;

以及我正在运行的python文件

import example as e

def testCB(a, b):
    print "running in CB: " +str(b)
    # what can I do with a


d = e.Die()
p = d.getDie(2)
print "hello i'm here"
p.foo2(4)
d.funcTest(testCB, 4)

最后一行抛出一个错误,显然不能这样发送CB。在

如果回调是写在cpp/h文件中,我确实设法使用了回调,但是我想运行python回调函数。在

谢谢你的帮助!在


Tags: 文件函数includeexample线程fileintshared

热门问题