<legend id='2fcWO'><style id='2fcWO'><dir id='2fcWO'><q id='2fcWO'></q></dir></style></legend>
<i id='2fcWO'><tr id='2fcWO'><dt id='2fcWO'><q id='2fcWO'><span id='2fcWO'><b id='2fcWO'><form id='2fcWO'><ins id='2fcWO'></ins><ul id='2fcWO'></ul><sub id='2fcWO'></sub></form><legend id='2fcWO'></legend><bdo id='2fcWO'><pre id='2fcWO'><center id='2fcWO'></center></pre></bdo></b><th id='2fcWO'></th></span></q></dt></tr></i><div id='2fcWO'><tfoot id='2fcWO'></tfoot><dl id='2fcWO'><fieldset id='2fcWO'></fieldset></dl></div>
  • <tfoot id='2fcWO'></tfoot>

      • <bdo id='2fcWO'></bdo><ul id='2fcWO'></ul>

        <small id='2fcWO'></small><noframes id='2fcWO'>

        使用 boost::python 将回调从 python 传递到 C++

        pass callback from python to c++ using boost::python(使用 boost::python 将回调从 python 传递到 C++)
      1. <small id='vmRZ5'></small><noframes id='vmRZ5'>

        <i id='vmRZ5'><tr id='vmRZ5'><dt id='vmRZ5'><q id='vmRZ5'><span id='vmRZ5'><b id='vmRZ5'><form id='vmRZ5'><ins id='vmRZ5'></ins><ul id='vmRZ5'></ul><sub id='vmRZ5'></sub></form><legend id='vmRZ5'></legend><bdo id='vmRZ5'><pre id='vmRZ5'><center id='vmRZ5'></center></pre></bdo></b><th id='vmRZ5'></th></span></q></dt></tr></i><div id='vmRZ5'><tfoot id='vmRZ5'></tfoot><dl id='vmRZ5'><fieldset id='vmRZ5'></fieldset></dl></div>
          • <bdo id='vmRZ5'></bdo><ul id='vmRZ5'></ul>
              <legend id='vmRZ5'><style id='vmRZ5'><dir id='vmRZ5'><q id='vmRZ5'></q></dir></style></legend><tfoot id='vmRZ5'></tfoot>

                    <tbody id='vmRZ5'></tbody>
                  本文介绍了使用 boost::python 将回调从 python 传递到 C++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想将回调从我的 python 代码传递给 C++

                  I want to pass callback from my python code to c++

                  我希望我的代码看起来像这样:在 C++ 中:

                  I want my code look something like this: In C++ :

                  typedef void (*MyCallback_t) (CallbackInfo);
                  
                  class MyClass
                  {...
                     void setcallback(MyCallback_t cb);
                   ...
                  }
                  

                  并在 python 中使用它:

                  And to use it in python :

                  import mylib
                  
                  def myCallback(mylib_CallbackInfo):
                  ...
                  
                  t = mylib.MyClass()
                  t.setcallback(myCallback)
                  

                  我在我的问题附近看到了一些主题,但无法解决

                  I saw some topics near my problem but couldn't solve it

                  例如这里:使用 Python 和 C++ 进行实时处理和回调使用 boost::python 和关于 GLI 的警告,但没有示例.还有这里

                  For example here : Realtime processing and callbacks with Python and C++ there is advice to use boost::python and warning about GLI but no examples. And here

                  如何调用python来自外语线程(C++)的函数没有完整的python代码部分和BOOST_PYTHON_MODULE"部分的描述

                  How to call a python function from a foreign language thread (C++) there is no full description with python code part and with "BOOST_PYTHON_MODULE" part

                  我还在 Boost python howto 但它没有编译,实际上我无法理解如何使用它.

                  I also found link to use py_boost_function.hpp for example in Boost python howto but it didn't compile and actualy I couldn't understand how to use it.

                  推荐答案

                  好的,我也在努力解决这个问题,但到目前为止,这里对我有用:

                  Ok, I'm still trying to figure this out too, but here's whats working for me so far:

                  #this is the variable that will hold a reference to the python function
                  PyObject *py_callback;
                  
                  #the following function will invoked from python to populate the call back reference
                  PyObject *set_py_callback(PyObject *callable)
                  {
                      py_callback = callable;       /* Remember new callback */
                      return Py_None;
                  }
                  ...
                  #Initialize and acquire the global interpreter lock
                  PyEval_InitThreads();
                  
                  #Ensure that the current thread is ready to call the Python C API 
                  PyGILState_STATE state = PyGILState_Ensure();
                  
                  #invoke the python function
                  boost::python::call<void>(py_callback);
                  
                  #release the global interpreter lock so other threads can resume execution
                  PyGILState_Release(state);
                  

                  python 函数从 C++ 调用,并按预期执行.

                  The python function is invoked from C++, and executes as expected.

                  这篇关于使用 boost::python 将回调从 python 传递到 C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is Type(::x); valid?(是类型(::x);有效的?)
                  Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                  Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                  Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                  No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                  Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)
                  <legend id='DZVeY'><style id='DZVeY'><dir id='DZVeY'><q id='DZVeY'></q></dir></style></legend>
                    <bdo id='DZVeY'></bdo><ul id='DZVeY'></ul>

                  • <tfoot id='DZVeY'></tfoot>
                        <tbody id='DZVeY'></tbody>

                          • <small id='DZVeY'></small><noframes id='DZVeY'>

                          • <i id='DZVeY'><tr id='DZVeY'><dt id='DZVeY'><q id='DZVeY'><span id='DZVeY'><b id='DZVeY'><form id='DZVeY'><ins id='DZVeY'></ins><ul id='DZVeY'></ul><sub id='DZVeY'></sub></form><legend id='DZVeY'></legend><bdo id='DZVeY'><pre id='DZVeY'><center id='DZVeY'></center></pre></bdo></b><th id='DZVeY'></th></span></q></dt></tr></i><div id='DZVeY'><tfoot id='DZVeY'></tfoot><dl id='DZVeY'><fieldset id='DZVeY'></fieldset></dl></div>