Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling detaching a window when importing a module doesn't work #62

Closed
mrzv opened this issue May 8, 2016 · 4 comments
Closed

Calling detaching a window when importing a module doesn't work #62

mrzv opened this issue May 8, 2016 · 4 comments

Comments

@mrzv
Copy link
Contributor

mrzv commented May 8, 2016

Not sure what's causing it, but when I try to call mainloop(detach = ...) during the initialization of my own module exposed using pybind11, not only does it not detach, but it hangs even after I close the window. Here's an example code:

struct MyScreen: public ng::Screen
{
    MyScreen():
        ng::Screen({ 1200, 800 }, "Test")           {}
};

struct Environment
{
    Environment(py::module m, py::module nanogui): nanogui_(nanogui)
    {
        py::object ng_init     = nanogui_.attr("init");
        py::object ng_mainloop = nanogui_.attr("mainloop");

        ng_init();

        auto* viewer = new MyScreen;
        viewer->drawAll();
        viewer->setVisible(true);

        py::object viewer_obj = py::cast(viewer);
        m.attr("viewer") = viewer_obj;

        py::tuple args;
        py::dict kwargs;
        kwargs["detach"] = viewer_obj;

        handle_ = ng_mainloop(*args, **kwargs);
    }

    ~Environment()
    {
        py::object ng_shutdown = nanogui_.attr("shutdown");
        py::object handle_join = handle_.attr("join");
        handle_join();
        ng_shutdown();
    }

    py::module nanogui_;
    py::object handle_;
};

PYBIND11_PLUGIN(mymod)
{
    py::module m("mymod", "mymod bindings");

    auto nanogui = py::module::import("nanogui");
    py::object screen = nanogui.attr("Screen");

    py::class_<MyScreen>(m, "MyScreen", screen);

    py::class_<Environment>(m, "Environment");

    m.attr("_env") = py::cast(new Environment(m,nanogui));

    return m.ptr();
}
@mrzv
Copy link
Contributor Author

mrzv commented May 8, 2016

Same effect can be achieved with the following Python code:

# mymod.py

import nanogui as ng

ng.init()

s = ng.Screen(ng.Vector2i(1200,800), "Test")
s.drawAll()
s.setVisible(True)

h = ng.mainloop(detach = s)

Then import mymod causes the same problem as the C++ code above.

@wjakob
Copy link
Owner

wjakob commented May 8, 2016

I get

 % python2.7 
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import mymod
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: not holding the import lock
>>>

Apparently a thread that imports a Python module must also hold a special import lock. This of course wreaks havoc when switching threads beneath Python.

http://stackoverflow.com/questions/4754319/what-is-import-lock-in-python
http://bugs.python.org/issue18122

I think this is a bit too weird of an issue to consider a pybind11/nanogui bug.

@wjakob
Copy link
Owner

wjakob commented May 8, 2016

(The solution is to import your module, and to postpone the mainloop call to a method that is invoked following the import.)

@mrzv
Copy link
Contributor Author

mrzv commented May 8, 2016

Actually, it seems to also be some weird interaction with IPython. In plain Python, I also get "not holding import lock", but actually everything seems to function fine. In IPython, I don't get the RuntimeError, but I do get the hanging behavior.

Python has some weird quirks. But I agree with you, it's not worth considering a pybind11/nanogui bug.

@wjakob wjakob closed this as completed May 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants