Initial delay in web-app after page loads
See original GitHub issueHi,
After watching Brad Traversy’s crash course on brython, I decided to code a web-app using it. It is a calculator app with some math functions. You can find it here. The problem is that after the web page loads, the app does not respond until nearly 50s or so. This happens only the first time - on reload, there is no delay. To recreate the issue, you would need to clear the browser’s cache and then reload the page.
My repo is here . I am importing only the functools
and math
standard library Python modules. I am not using any cdn - all files are local.
Please help me debug this problem.
Thanks,
Debashish
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Web app initial load time - Stack Overflow
The delay you are seeing is due to the server being busy and your page is sitting in a queue waiting behind other...
Read more >Page load timing process | New Relic Documentation
A page load begins when a user selects a hyperlink, submits a form, or types a URL in a browser. This is also...
Read more >10 Reasons for Slow Website Loading (With Solutions)
Understand these 10 core reasons for slow website loading and learn how to resolve these issues & your traffic, revenue, and credibility.
Read more >Recommended Web Performance Timings: How long is too ...
There are no clear set rules as to what constitutes a slow pace when loading pages, but there are specific guidelines for indicating...
Read more >Learn about page loading performance data (web apps)
Note: Performance Monitoring only records the first input delay metric when a user clicks on the web page within the first 5 seconds...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Sorry in advance for the long answer, but the problem is one of the 2 most difficult challenges Brython has to face (the other being generators, a nightmare !).
1. some modules require many, many other modules
Your code only has 3 imports : modules
browser
andmath
don’t import anything else, but it’s not the same forfunctools
:functools
must import modules_thread, collections, reprlib, weakref, _functools
at module level, and may import modulestyping, abc, reprlib, types, warnings
inside functionswarnings
must importsys, traceback, linecache, re, tracemalloc
re
must importsre_constants, copyreg, sre_parse, _locale, functools, enum, sre_compile
and so on, and so on. In the end, just because of
functools
you end up having to precompile about 64% of the standard library (brython_modules.js
for your application is 2,3 MB compared to 3,6 forbrython_stdlib.js
).The most frustrating is that in fact your application only uses 17 modules, because it doesn’t need any of the modules that
functools
may import ! But since it’s impossible to know in advance if they will be used, all modules have to be precompiled to JS and stored in the cache.2. ideal solution, and why it’s not possible 😦
Ideally, translating to Javascript and storing in cache could happen at runtime : when Brython runs
the algorithm could be:
a
is in the cachea
, translate to Javascript, store in indexedDBUnfortunately this is not possible because, by design, access to indexedDB is asynchronous (non-blocking) : storing into or reading from the database doesn’t stop the program execution, which means that the next instruction (“print(a.x)”) is executed just after the asynchronous call is done, not when this call completes. Of course, since “import a” is not completed, “print(a.x)” fails.
The only possible thing (at least the only one I found…) is to precompile and store in cache before starting to run the program. And with hundreds of modules to compile and store, it takes dozens of seconds…
3. workarounds
There are at least 2 workarounds (I ignore the radical solution of not using
functools
at all 😉false
:In this case, the cache is not used and all modules are compiled to Javascript at each run. All runs take about the same time, which is much less than the first import with indexedDB but more than with the next runs with the cache
style.display = "none"
) and show a DIV with something like “Loading, please wait, it will be faster next time…” (maybe with a timeout of 1 or 2 seconds so that it will show only in the first run)I am conscious that this is not ideal, if someone can think of a better solution I would be extremely pleased !
@PierreQuentel Thanks a lot for taking the trouble of clearing my doubts! I am going to close the issue.