nicegui: Wrapping the menu_and_tabs example into a @ui.page("/") causes continuous 100% CPU load

Description

When I take the menu_and_tabs example and wrap it into a @ui.page("/") like below, the process then runs with continous 100% CPU load when Tab A or Tab C are active. The CPU load drops to 0% when Tab B is active. The issue doesn’t persist for the original menu_and_tabs example.

I reproduced the issue in a fresh .venv with nicegui==1.4.13 under Python 3.10 and 3.12

from nicegui import ui
@ui.page("/")
def index_page() -> None:

    with ui.header().classes(replace='row items-center') as header:
        ui.button(on_click=lambda: left_drawer.toggle(), icon='menu').props('flat color=white')
        with ui.tabs() as tabs:
            ui.tab('A')
            ui.tab('B')
            ui.tab('C')

    with ui.footer(value=False) as footer:
        ui.label('Footer')

    with ui.left_drawer().classes('bg-blue-100') as left_drawer:
        ui.label('Side menu')

    with ui.page_sticky(position='bottom-right', x_offset=20, y_offset=20):
        ui.button(on_click=footer.toggle, icon='contact_support').props('fab')

    with ui.tab_panels(tabs, value='A').classes('w-full'):
        with ui.tab_panel('A'):
            ui.label('Content of A')
        with ui.tab_panel('B'):
            ui.label('Content of B')
        with ui.tab_panel('C'):
            ui.label('Content of C')

ui.run()

edit: in the meantime I have reproduced the issue on a second machine. The smallest code to reproduce the problem appears to be the ui.tabs themselves

from nicegui import ui

@ui.page("/")
def index_page() -> None:
    
    with ui.tabs():
        ui.tab('A')
        ui.tab('B')
        ui.tab('C')
        ui.tab('D')

ui.run()

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15 (11 by maintainers)

Commits related to this issue

Most upvoted comments

Yah, I saw those when trying to figure this out. This seems to be a more serious bug though as I’m seeing a complete collapse in the functioning of asyncio.sleep. It goes from returning in 10-11ms as specified in the call to returning in < 50μs and the task resuming in the next asyncio loop iteration. Thats 10,000μs vs 50. A factor of 200 isn’t just an inaccuracy in the Windows clock. In this case, it’s the difference between <1% CPU usage and 100%. And there’s also the issue of it toggling between completely normal behavior and non-functional behavior at random. But yes, hopefully they fix it and, until then, at least it’s documented here that any Windows users encountering this issue can try changing

await asyncio.sleep(0.01) 

to

await asyncio.sleep(0.016) 

in the Outbox.loop() function of outbox.py. This completely resolved the issue for me.

Love this project so just wanted to do my part. It would be interesting to know if this solution worked for @pplno or @oidex. I have a feeling it’s affecting many others without them realizing it as you’d have to be monitoring your CPU usage, notice your fans spinning up, or your battery draining because the NiceGUI interface does continue to function.

I am so happy this is fixed… The howling CPU almost non stop would have made me mad long ago if not for the noise cancelling headphones! Thanks guys!

Newer CPython issue link (after migration to GitHub): https://github.com/python/cpython/issues/75720

For number 2 (removing the check_interval) I created PR #2827. But since this PR solves only part of this issue, I didn’t link both tickets so that the issue doesn’t get auto-closed. Sorry for the back-and-forth.

Great! I’m looking forward to your pull request. You can focus on number 1 and I’ll add 2 and maybe 3 later. 🙂

@falkoschindler Wow, Number 1 is exactly what I was going to submit. It’s done, just need to fully test. It has the added benefit that it drops CPU usage when idle by 80% on my system. Then 3 would not be necessary but may help avoid future issues with the broken asyncio.sleep method on Windows.

I stumbled over the same or a very similar problem. I was trying to create a page with many cards (~200) that consisted of an image loaded from an url and a title. When the initial request was done and the all the elements were visible (except the images which are loaded lazily) the page froze completely. Even Firefox showed a popup notifying me about a performance issue with the page and advised to stop the process. Similar to the described issue one CPU core is maxed out for the duration of the freeze (~30s).

I wrote this simple showcase to see if it made a difference if the mentioned @ui.path() decorator was used. The navbar / header is mainly there to see if the page is stuck, as you can see that very easily by hovering the mouse over a tab or button and see if it focuses correctly when hovering.

from nicegui import ui

def spam():
    with ui.header().classes(replace='row items-center') as header:
        ui.button(on_click=lambda: left_drawer.toggle(), icon='menu').props('flat color=white')
        with ui.tabs() as tabs:
            ui.tab('A')
            ui.tab('B')
            ui.tab('C')
        with ui.link(target="/foo"):
            ui.button("Foo")

    with ui.row():
        for i in range(200):
            with ui.link(target=f"/doc/{i}"):
                with ui.card().tight().classes("w-72"):
                    ui.image(f"https://picsum.photos/300/200?{i}").classes("h-full")
                    # element.get_img_ui(7)
                    with ui.card_section():
                        ui.label(f"Title {i}")


@ui.page("/foo")
def foo():
    spam()


spam()

ui.run()

And indeed there is a significant difference:

CPU Usage on / (Without @ui.path()) nicegui-without-path

CPU Usage on /foo (With @ui.path()) - Stuck for ~15s nicegui-with-path

I’ve conducted the tests on Linux Mint 21 (uses Ubuntu 22.04 LTS as base) with Firefox 121 and an AMD Ryzen 5 5600G.

I’ve used the Firefox Profiler for making a recording of the calls during the loading when the problem was appearing (different code than the one above). I disabled prod_js so that the method names are meaningful.

https://share.firefox.dev/3SPlbGz