lundi 22 octobre 2018

Using Pybind11 with third-party modules

I have a small Python script, that uses numpy for computations few times and Qt Application, that invokes a script using pybind11. Usingpy::scoped_interpreter to manage interpreter lifetime I'm able to run a script only once; on second call application crashes.

Here I found a solution - managing interpreter lifetime manually with initialize_interpreter() / finalize_interpreter() pair can solve that problem, but not in my case. Now Application runs a script,returns a result and crashes on finalize_interpreter() line. Removing this line leads to the same behaviour as using py::scoped_interpreter. Here's used code:

//    py::scoped_interpreter guard{};/*crashes on second call if using numpy*/
py::initialize_interpreter();
py::module FullPow = py::module::import("FullPow");//importing module with script
const char* FilePath = filename.toStdString().c_str();
if (filename != ""){
    py::list result = FullPow.attr("PybindExample")(FilePath);
    vector<float> n = result.cast<vector<float>>();
    qDebug()<<n[1];}
py::finalize_interpreter();

So is there any way to use third-party modules like numpy more than one times? Will appreciate any suggestions!

Aucun commentaire:

Enregistrer un commentaire