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

Reload slow, no response to ctrl-C on Windows #3652

Open
superlou opened this issue Aug 30, 2024 · 14 comments
Open

Reload slow, no response to ctrl-C on Windows #3652

superlou opened this issue Aug 30, 2024 · 14 comments
Labels
help wanted Extra attention is needed

Comments

@superlou
Copy link

superlou commented Aug 30, 2024

Description

A simple application starts up in a second or two, but on making a change, reloading may take 10 seconds or more and may hang. Additionally, when hung, the terminal (both PowerShell and CMD) has no response to ctrl-c or clicking the close button. It's been about a year since I've used NiceGUI on Windows (I mostly use Linux) and I don't remember this behavior before.

It sounds a bit like #3344, but this isn't in PyCharm. There are also some issues in uvicorn like encode/uvicorn#1972 that sound related, but I know enough about the NiceGUI stack to know if that could be the root cause.

My test script is:

from nicegui import ui

ui.button("Button 1")
ui.button("Button 2")
ui.button("Button 3")

ui.run()

Steps to recreate:

  1. Make a simple NiceGUI script like the above named simple.py.
  2. Run python simple.py in a Windows terminal.
  3. Comment out one of the ui.button(...) calls and save the file.
  4. Wait for "... Reloading..." to complete and show "NiceGUI ready to go..."

Environment:

  • Windows 11 (also Windows 10, but mostly testing on Windows 11)
  • Python 3.12.4 (x64)
  • nicegui 1.4.37
  • uvicorn 0.30.6
  • fastapi 0.109.2

Edit: I tried the same test in WSL2 on the same computer and reloads are very fast, under a second.

@rodja
Copy link
Member

rodja commented Aug 30, 2024

Wow. That sounds serious. We aim for a super fast reload. Can anyone else with Windows reproduce this?

@python-and-novella
Copy link
Contributor

@superlou do you test it in pure termial or in ide's integrated terminal? It soulds like similar bug in pycharm or in vscode i met.

@superlou
Copy link
Author

superlou commented Sep 1, 2024

I was testing in a pure terminal, both Powershell and CMD had similar behavior (as well as Microsoft Terminal running Powershell).

@falkoschindler falkoschindler added the help wanted Extra attention is needed label Sep 2, 2024
@frankhuurman
Copy link
Contributor

@superlou
This might be a bit skewed because I'm using Windows 10 in a VM but for me with the nicegui version you specified and running your code example in cmd.exe it takes about 5 seconds on average to reload the page during file saves.
Using Git Bash takes about 4 seconds on average though it has trouble handling Ctrl + C for me as well.

On Linux it's nearly instant for me so seems to be a very Windows-y problem.

Upgrading to the latest NiceGUI version doesn't seem to change anything speed-wise either so it might be related to the way filewatcher picks up on filechanges on Windows in comparison to the other OS-es?

@superlou
Copy link
Author

@frankhuurman, I don't think it's filewatcher, only because when I save, it immediately pops up with the notification that a file changed. The issues happen in the delay after.

Does nicegui use uvicorn logging? This comment describes my ctrl-c behavior.

@rodja
Copy link
Member

rodja commented Sep 16, 2024

Interesting @superlou! Uvicorn logging can be disabled with ui.run(uvicorn_logging_level='critical').

@superlou
Copy link
Author

superlou commented Sep 16, 2024

Just got a chance to try it with logging disabled. No change unfortunately. Was really hoping it was something straightforward.

from nicegui import ui

ui.button("Button 1")
ui.button("Button 2")
ui.button("Button 3")

ui.run(uvicorn_logging_level='critical')

It's still 5 to 10 seconds to reload (if it does) when commenting and uncommenting buttons. I'm pretty sure it's reacting to uvicorn_logging_level, since I don't get the "WARNING: WatchFiles detected changes in 'main.py'. Reloading..." message with the logging level set to critical.

@frankhuurman
Copy link
Contributor

Just noticed that you're using Python 3.12.
I opened a NiceGUI issue a while back that's related to the Python 3.12 and Windows combination that might be similar to your issue: #2138

Could you try your same code using Python 3.11 on Win 11?

Back then it seemed to be an upstream problem that needed to be fixed but I assumed that would've been done by now, but I'm curious if downgrading your Python version solves the issue for now.

@superlou
Copy link
Author

I used uv to test with Python 3.11.9, and it's the same behavior. WatchFiles detects changes immediately, but it takes about 15 seconds to respond in the browser.

@NebularNerd
Copy link

I'm just starting out with NiceGUI on Win11 running Python 3.10 and have had similar issues. Same as @superlou I'll start the server, make a change in VSCODE and it sometimes will reload ok, but it's 50/50 between that or just locking up and needing me to kill Python in the task manger.

Running it via Powershell in Microsoft Terminal if that helps.

@genotrance
Copy link

I'm seeing the same issue on Windows - reloads (and loads) are super slow and frustrating.

Python 3.12.7
nicegui 2.5.0
fastapi 0.115.4
uvicorn 0.32.0

@superlou
Copy link
Author

I'm not sure this is the root cause, but is there a way to try the solution suggested in encode/uvicorn#1972 (comment) to see if it's something related to filewatches in uvicorn? I'm not sure how to configure nicegui to test that.

@falkoschindler
Copy link
Contributor

@superlou This works for me on Mac:

watchfiles --filter python "python main.py"

@superlou
Copy link
Author

superlou commented Dec 2, 2024

Unfortunately, when I edit the test file (same process as earlier by commenting/un-commenting buttons) on Windows, either the page doesn't update or I get a permissions error:

PS C:\workspace\nicegui-test> watchfiles --filter python "python main.py"
[08:27:34] watchfiles v0.24.0 👀  path="C:\workspace\nicegui-test" target="python main.py" (command) filter=PythonFilter...
NiceGUI ready to go on http://localhost:8080, http://192.168.40.71:8080, and http://192.168.56.1:8080
[08:27:40] 1 change detected
NiceGUI ready to go on http://localhost:8080, http://192.168.40.71:8080, and http://192.168.56.1:8080
[08:27:48] 1 change detected
NiceGUI ready to go on http://localhost:8080, http://192.168.40.71:8080, and http://192.168.56.1:8080
[08:27:53] 1 change detected
NiceGUI ready to go on http://localhost:8080, http://192.168.40.71:8080, and http://192.168.56.1:8080
WARNING:  WatchFiles detected changes in 'main.py'. Reloading...
[08:28:05] 1 change detected
Traceback (most recent call last):
  File "C:\Users\lsimons\AppData\Local\Programs\Python\Python312\Lib\site-packages\watchfiles\run.py", line 149, in run_process
    process.stop(sigint_timeout=sigint_timeout, sigkill_timeout=sigkill_timeout)
  File "C:\Users\lsimons\AppData\Local\Programs\Python\Python312\Lib\site-packages\watchfiles\run.py", line 327, in stop
    os.kill(self.pid, signal.SIGINT)
KeyboardInterrupt

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\lsimons\AppData\Local\Programs\Python\Python312\Scripts\watchfiles.exe\__main__.py", line 7, in <module>
  File "C:\Users\lsimons\AppData\Local\Programs\Python\Python312\Lib\site-packages\watchfiles\cli.py", line 171, in cli
    run_process(
  File "C:\Users\lsimons\AppData\Local\Programs\Python\Python312\Lib\site-packages\watchfiles\run.py", line 153, in run_process
    process.stop()
  File "C:\Users\lsimons\AppData\Local\Programs\Python\Python312\Lib\site-packages\watchfiles\run.py", line 327, in stop
    os.kill(self.pid, signal.SIGINT)
PermissionError: [WinError 5] Access is denied

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

7 participants