title: boost::python - how to invoke a python function in its own thread from C++?
body: <p>I have a module written in python. this module is sort of an interface to many different functionalities I implemented in Python:</p>
  
  <p>EmbeddingInterface.py simply imports this module and creates an instance:</p>
  
  <pre><code>import CPPController
  
  cppControllerInstance = CPPController()
  </code></pre>
  
  <p>I would like to use cppControllerInstance in c++. this is what I have done so far:</p>
  
  <pre><code>#include &lt;Python.h&gt;
  #include &lt;boost\python.hpp&gt;
  
  using namespace boost;
  
  python::object createController()
  {
      try
      {
          Py_Initialize();
  
          python::object mainModule = python::import("__main__");
          python::object mainNamespace = mainModule.attr("__dict__");
  
          python::dict locals;
  
          python::exec(
              "print \"loading python implementetion:\"\n"
              "import sys\n"
              "sys.path.insert(0, \"C:\\Projects\\Python\\ProjectName\\Panda\")\n"
              "import EmbeddingInterface\n"
              "controller = EmbeddingInterface.cppControllerInstance\n",
              mainNamespace, locals);
  
              python::object controller = locals["controller"];
              return controller;
      }
      catch(...) {}
  }
  </code></pre>
  
  <p><strong>The Problem:</strong></p>
  
  <p>This 'controller' has some functions which must be called asynchronously.
  its work is continuous and in addition it can throw exceptions.
  which is why std::async sounded great.</p>
  
  <p>But it doesn't work:</p>
  
  <pre><code>int main()
  {
      python::object controller = createController();
      python::object loadScene = controller.attr("loadScene");
      //loadScene(); // works OK but blocking!
      std::async(loadScene); // non blocking but nothing happens!
      while(true); // do some stuff
  }
  </code></pre>
  
  <p>I tried to invoke the python function 'loadScene' with its own thread but the function seemed to be blocking. It never returns.</p>
  
  <p>What is the proper way of doing that?</p>