Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

binding_refresh_interval not having any impact #57

Closed
bapowell opened this issue Aug 10, 2022 · 7 comments
Closed

binding_refresh_interval not having any impact #57

bapowell opened this issue Aug 10, 2022 · 7 comments

Comments

@bapowell
Copy link

Running on a Raspberry Pi, NiceGUI is fairly CPU intensive (although still usable). I tried changing the binding_refresh_interval value, via a ui.run() parameter, but it doesn't seem to make any difference on CPU usage.

@falkoschindler
Copy link
Contributor

As far as I can tell, binding_refresh_interval does have an effect. Adding a print statement within the loop() in binding.py demonstrates the impact of that interval. I tried it with the following example:

from nicegui import ui

class Model:
    def __init__(self):
        self.x = 1

model = Model()
ui.number('x').bind_value_from(model, 'x')

ui.run(binding_refresh_interval=0.5)

There is probably something else CPU-intensive going on in your code. Can you boil it down to some minimum example?

@bapowell
Copy link
Author

I think what I'm seeing is essentially the minimum CPU usage from NiceGUI. In other words, using your very minimal example, I see roughly 50% CPU usage on one of the RPi's cores. Changing the binding_refresh_interval has no impact.

What's interesting is that I can run JustPy, using the example at the end of https://justpy.io/tutorial/model_and_data/, which is also doing bind operations, and it runs with less than 5% CPU usage on one core.

Please understand this isn't a knock against NiceGUI, since it has a much more "batteries included" approach, which I can appreciate. But a 10x increase in CPU usage seems excessive, and I wonder if there's something I'm missing.

@rodja
Copy link
Member

rodja commented Aug 18, 2022

We will run our examples on an RPi in the next days and come back to you.

@me21
Copy link
Contributor

me21 commented Aug 20, 2022

I'm running my nicegui on the Beaglebone Black single board computer, it has one CPU core and it uses 20% give or take.
During startup, it loads CPU fully, but I can live with that.

@falkoschindler
Copy link
Contributor

I just checked the CPU usage on a Raspberry Pi 3 Model B V1.2 (2015) with a simple hello-world script:

from nicegui import ui

ui.label('Hello world!')

ui.run()

Here are my conclusions:

  • reload=True (the default) can be costly. Here is a related discussion over at uvicorn: High CPU usage when using --reload encode/uvicorn#338. If in doubt, try using reload=False.
  • It might also help to limit the amount of files that need to be watched: Originally I tested with a test script in my home folder. This causes uvicorn to watch the whole home directory ramping the CPU up to 100 %. After moving the script into a separate folder, uvicorn had to watch only a single file, lowering the CPU to less than 1 %.
  • binding_refresh_interval does not have any significant impact for this tiny script.

So if you don't need auto-reload, set it to False. Otherwise, keep the watched directory small (and/or use reload_includes, reload_excludes and reload_dirs).

Note that changes to the arguments of ui.run are not caught by the auto-reload but require restarting the script completely.

@bapowell
Copy link
Author

You nailed it, @falkoschindler.

I had my venv in the same folder as my test script. Changing reload to False lowered CPU to around 1%, as did moving the script to its own folder (keeping reload=True).

@me21
Copy link
Contributor

me21 commented Aug 24, 2022

In my case, running it on Beaglebone Black, changing refresh interval from 0.1 to 0.5 lowered CPU load from 60% to 10% approximately.
So on weaker CPU it does matter too.

I mean I ran my large script for UI, not the "hello world" script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants