pybind11-来自不同线程的调用函数

发布于 2025-02-11 05:52:29 字数 831 浏览 0 评论 0原文

我在亲爱的imgui中具有控制台

python错误通常显示在控制台中

static std::string in;
if (ImGuiCP::InputText("##InputCommand", &in)) {
    py::initialize_interpreter();
    try {
        py::exec(in);
    } catch (pybind11::error_already_set &e) {
        cout << e.what();
    }
    py::finalize_interpreter();
}

这种

py::initialize_interpreter();

代码在没有

static std::string in;
if (ImGuiCP::InputText("##InputCommand", &in)) {
    try {
        py::exec(in);
    } catch (pybind11::error_already_set &e) {
        cout << e.what();
    }
}

错误的情况下工作, 正常执行,如果错误在Python中,则处理捕获量,即,它在控制台中显示错误,此后该应用程序紧密冻结

I have console in Dear ImGui

such code works without errors, python errors are normally displayed in the console

static std::string in;
if (ImGuiCP::InputText("##InputCommand", &in)) {
    py::initialize_interpreter();
    try {
        py::exec(in);
    } catch (pybind11::error_already_set &e) {
        cout << e.what();
    }
    py::finalize_interpreter();
}

but I don't need to create a new interpreter every time, I want to create it once

in main I write

py::initialize_interpreter();

in imgui code

static std::string in;
if (ImGuiCP::InputText("##InputCommand", &in)) {
    try {
        py::exec(in);
    } catch (pybind11::error_already_set &e) {
        cout << e.what();
    }
}

valid code is executed normally, and if the error is in python, it handles the catch, that is, it displays the error in the console, and after that the application freezes tightly

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文