• C++通过Swig跨线程回调Python代码


    C++ 定义 Callback 类. PyThreadStateLock 保证垮线程调用成功:

     

    #include <Python/Python.h>
    class Callback
    {
    public:
        Callback(){}
        
        virtual ~Callback(){}
        virtual void call(ObjWithPyCallback& object){}
    };
    
    class ObjWithPyCallback
    {
    public:
        
        ObjWithPyCallback();
        ~ObjWithPyCallback();
        void setCallback(Callback &callback);
        void call(); 
        
    private:
        void NotifyThread();
        Callback* callback_;
    };
    class PyThreadStateLock { public: PyThreadStateLock(void) { state = PyGILState_Ensure( ); } ~PyThreadStateLock(void) { PyGILState_Release( state ); } private: PyGILState_STATE state; };

    Callback.cpp

    ObjWithPyCallback::ObjWithPyCallback() : callback_(NULL)
    {
        Py_Initialize();
        PyEval_InitThreads();
        PyEval_ReleaseLock();
    }
    
    ObjWithPyCallback::~ObjWithPyCallback()
    {    
    }
    
    void ObjWithPyCallback::setCallback(Callback &callback)
    {
        callback_ = &callback;
    }
    
    void ObjWithPyCallback::call()
    {
        if ( ! callback_ )
        {
            std::cerr << "No callback is set.
    ";
        }
        else
        {
            thread(&ObjWithPyCallback::NotifyThread, this).detach();//call in another thread
            //callback_->call(*this);//call directly
            
        }
    }
    
    void ObjWithPyCallback::NotifyThread()
    {
        class PyThreadStateLock PyThreadLock;//fix segmentation fault 
        callback_->call(*this);
    }

    .i 文件中

    %module(directors="1") cb
    %{
       #include "Callback.h"
    %}
    %feature("director") Callback;
    
    %include "Callback.h"

    在 python 中调用:

    import cb
    
    class CB(cb.Callback):
        def __init__(self):
            super(CB, self).__init__()
        def call(self, x):
            print("Hello from CB!")
            print(x)
    
    o = cb.ObjWithPyCallback()
    mycb=CB()
    o.setCallback(mycb)
    o.call()
  • 相关阅读:
    java继承
    c#中委托和事件区别
    c#委托中的匿名方法和lambda表达式
    c#中内置委托
    iOS消息推送获取不到deviceToken解决方案
    python+appium+iOS自动化测试case如何写?
    Xcode查看iOS崩溃与崩溃日志分析
    iOS性能检测工具instrunments简单介绍
    python实现使用代码进行代理配置
    python+locust性能测试-最简单的登录点击次数
  • 原文地址:https://www.cnblogs.com/gujf2016/p/11671565.html
Copyright © 2020-2023  润新知