moderngl / moderngl-window

A cross platform utility library for ModernGL making window creation and resource loading simple
MIT License
244 stars 57 forks source link

Threading support #114

Closed totomobile43 closed 4 years ago

totomobile43 commented 4 years ago

Is it possible to implement running the window in a separate thread in order not to block execution? Are there plans to implement this feature? Is it feasible, and if so, what would be a good approach? For example, is it possible to use QThreadPool to manage this?

einarf commented 4 years ago

There are currently no plans to add any built in threading support. You can use threads any way you like as long as the thread that created the context/window is the only one accessing the context.

If you want even more control avoid using WindowConfig so you control your own main loop https://github.com/moderngl/moderngl-window/blob/master/examples/custom_config_class.py https://github.com/moderngl/moderngl-window/blob/master/examples/custom_config_functions.py

A lot of times you can get away with not using threads depending on what you want to achieve.

totomobile43 commented 4 years ago

Thanks for the quick reply! I tried your suggestions (just launching the sample example custom config class in a new thread). I tried this on macOS, but its not possible to launch OpenGL windows in a non-main thread. The Windows side also seems to have its own issues (having a quick look at pyrender/pyglet seems to show issues for running in thread). It might be do-able in Linux but its not really worth it if it doesn't work smoothly across platforms. I would close the issue unless you have some other ideas.

einarf commented 4 years ago

I don't really have any good ideas around this. The cross platform issue is a big problem and we want to keep things simple. Even shared contexts have issues on different platforms. A valid use case is when creating several headless contexts with EGL backend using different gpus (device_id parameter) in a multi-gpu setup.

We have thought about providing something for asyncio at least.