How to cut down startup time in half while native=True #3363
Closed
EmberLightVFX
started this conversation in
Ideas / Feature Requests
Replies: 1 comment
-
Thanks again, @EmberLightVFX! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I found a bug some days ago where NiceGUI re-initializes itself when you have native=True enabled (while refresh=False), meaning the startup time is double compared to native=False.
You can find my issue here: #3356
Maybe my proposal on how to fix this suits the discussion forum better than my issue so I'm posting here also :)
The issue:
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.
The fix:
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!
My fix takes my execution time from 6 seconds down to 3 seconds!
The solutions/proposals:
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.
I would love to hear thoughts about this and if there is any interest for a PR with any of these fixes.
Beta Was this translation helpful? Give feedback.
All reactions