You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I noticed while running my NiceGUI script with native=True it seemed all code ran twice even tho reload=False.
I have seen this information: #794 (comment)
but non of those fixed the problem. NiceGUI is still re-initializing twice.
After a lot of digging I found what caused this problem.
Webview must run in a main thread, thus it's executed using multiprocessing so it gets its own thread.
The problem here is that a new multiprocessing thread inherits a copy of the parent process's memory, including all global variables and imported modules.
There is a new initialization of NiceGUI for some reason as it now lives in a new thread.
I tried encapsulating the NiceGUI import and the ui.run code under if __name__ == "__main__": and then move all Webview functions into a complete separate package.
If I simply moved all Webview functions into a new file inside the NiceGUI package all NiceGUI stuff gets re-imported because of everything inside the init.py
With these changes I finally get a window popping up directly after the first ui.run execution, making it as fast as native=False and cuts my startup time in half!
I'm not super familiar with NiceGUI's source code but these are my thoughts on how to implement this fix.
The easiest way is to move all Webview code into a separate package, maybe NiceGUI_native_view or something like that.
The harder way (but the one I personally like more) is to make sure nothing gets initialized on NiceGUI's import. This would require quite a refactor of the code but would give a lot of benefits.
More than fixing my problem above this would also make sure the GUI is only initialized when it's needed. The app I'm currently building works both in the terminal and with a GUI. Right now NiceGUI gets initialized even if I'm simply running it in the terminal. You wouldn't need to add if __name__ == "__main__": at a bunch of places to make sure NiceGUI isn't imported and used.
There would ether be a bunch of checks everywhere in the source code to see if NiceGUI has bin initialized. If not, initialize it. Functions would be in as good as all functions within __init__.py
The other way would be to add a app.initialize() function that would do this for us. The problem here is that all existing usages of NiceGUI would break and would need to be updated to add app.initialize() if they want to use the latest version of NiceGUI.
My fix takes my execution time from 6 seconds down to 3 seconds!
I would love to hear thoughts about this and if there is any interest for a PR with any of these fixes.
Thanks for bringing this up, @EmberLightVFX!
Cutting the startup time for native apps in half would indeed be a great improvement.
We're struggling to understand how your testtest module works. Maybe you can create a pull request so we play around with the code? Even though publishing a separate PyPI package wouldn't be desirable, it might be a starting point for a less drastic solution. Thanks!
@falkoschindler I made a PR with the test environment: #3365
I hope it works for you.
I added two tests. One slow (how it currently works) and one fast for how you need to write it with my separarte-module fix.
I named the separate module temp_webview that lives in the root dir.
I couldn't get a correct NiceGUI dev-environment up and running but I hope the PR works for you or at least shows you the setup.
Description
I noticed while running my NiceGUI script with
native=True
it seemed all code ran twice even thoreload=False
.I have seen this information: #794 (comment)
but non of those fixed the problem. NiceGUI is still re-initializing twice.
After a lot of digging I found what caused this problem.
Webview must run in a main thread, thus it's executed using multiprocessing so it gets its own thread.
The problem here is that a new multiprocessing thread inherits a copy of the parent process's memory, including all global variables and imported modules.
There is a new initialization of NiceGUI for some reason as it now lives in a new thread.
I tried encapsulating the NiceGUI import and the ui.run code under
if __name__ == "__main__":
and then move all Webview functions into a complete separate package.If I simply moved all Webview functions into a new file inside the NiceGUI package all NiceGUI stuff gets re-imported because of everything inside the init.py
With these changes I finally get a window popping up directly after the first ui.run execution, making it as fast as
native=False
and cuts my startup time in half!I'm not super familiar with NiceGUI's source code but these are my thoughts on how to implement this fix.
The easiest way is to move all Webview code into a separate package, maybe NiceGUI_native_view or something like that.
The harder way (but the one I personally like more) is to make sure nothing gets initialized on NiceGUI's import. This would require quite a refactor of the code but would give a lot of benefits.
More than fixing my problem above this would also make sure the GUI is only initialized when it's needed. The app I'm currently building works both in the terminal and with a GUI. Right now NiceGUI gets initialized even if I'm simply running it in the terminal. You wouldn't need to add
if __name__ == "__main__":
at a bunch of places to make sure NiceGUI isn't imported and used.There would ether be a bunch of checks everywhere in the source code to see if NiceGUI has bin initialized. If not, initialize it. Functions would be in as good as all functions within
__init__.py
The other way would be to add a app.initialize() function that would do this for us. The problem here is that all existing usages of NiceGUI would break and would need to be updated to add app.initialize() if they want to use the latest version of NiceGUI.
My fix takes my execution time from 6 seconds down to 3 seconds!
I would love to hear thoughts about this and if there is any interest for a PR with any of these fixes.
These are my test-scripts:
Slow script:
Fast script:
window.py
Create a new package called testtest with this file within (+ an empty __init__.py)
Modify native_module.py
Delete the _open_window function and add
from testtest import _open_window
in the topThe text was updated successfully, but these errors were encountered: