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
Sleep in Reloader loop to conserve CPU resources #2567
Conversation
Codecov ReportBase: 88.036% // Head: 88.012% // Decreases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## main #2567 +/- ##
=============================================
- Coverage 88.036% 88.012% -0.025%
=============================================
Files 71 71
Lines 5333 5339 +6
Branches 987 894 -93
=============================================
+ Hits 4695 4699 +4
Misses 461 461
- Partials 177 179 +2
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
Good catch. Thanks! |
This really should be configurable, and TBH I will accept it as an oversight by myself. https://github.com/sanic-org/sanic/blob/main/sanic/mixins/startup.py#L820 ☝️ This line should be something like: Reloader(..., primary.config.RELOADER_INTERVAL, ...) And then also have a default value, and type annotation. Would you be willing to add that to this PR? |
This is now configurable through The second commit is an attempt to make it the interval configurable through the CLI, however since I am not very familiar with this code base I am not entirely sure if I did it "right". While the code works, the setting is not actually visible in |
…nt.wait(interval)` instead of `sleep(interval)`, as sleeping blocks the thread and leaves the server hanging on shutdown.
…verride anything set in config
I am not sure about how good this CLI API feels, both in naming of the new arguments, and its verbosity: ~# sanic main:app --auto-reload --auto-reload-interval 5
~# sanic main:app -r -i 5
~# sanic main:app --reload --interval 5 Should setting the interval through the CLI implicitly enable the reloader, so that you only need to call it like this? ~# sanic main:app --auto-reload-interval 5 This works fine, but some ambiguity ("what interval?") would be introduced when using the short forms: ~# sanic main:app --interval 5
~# sanic main:app -i 5 Alternatively, instead of two separate arguments, maybe This would result in this API: ~# sanic main:app --reload 5.0
AUTO_RELOAD=True, AUTO_RELOAD_INTERVAL=5.0
~# sanic main:app --reload
AUTO_RELOAD=True, AUTO_RELOAD_INTERVAL=1.0 (default)
~# sanic main:app
AUTO_RELOAD=False, AUTO_RELOAD_INTERVAL=1.0 (default) |
Yeah, I like this.
I am not 100% sold on the short form. I suppose if we are allowing it to act implicit
This is a nice idea. Make sure to keep Of all the options, I like this the best.
|
IMO this needlessly bloats app.run and CLI API. Couldn't you put that in app.config instead? |
With the suggested API of rolling it into the existing It's supported in |
This pull request has been mentioned on Sanic Community Discussion. There might be relevant details there: https://community.sanicframework.org/t/on-intel-mac-the-sanic-dev-mode-uses-99-cpu/1077/2 |
You can set config also by environment variables, e.g. SANIC_RELOAD_INTERVAL=3 sanic your.app --reload All CLI options show up in CLI help, and thus I am vary of adding anything extra there. The You could also always set the interval via config, but it would only have an effect when the reloader is enabled. I suppose you wouldn't normally have use to ephemerally configure for different instantiations. In reality, I would expect there to be very little reason to ever change the value but I see that there could be some use to have it configurable, the same as all other timeouts and various other parameters are configurable. As an added benefit, you could |
I mean, the current changes in the pull request adds two lines to the CLI help, out of a total of 98 lines. The suggested change above would add zero lines to the CLI help (it'd just rewrite the help for the existing On the other hand, I do agree that there's likely very few reasons someone would want to set the reload interval to a non-default value. For really large large projects stored on a slow storage medium (e.g. HDDs), the default interval may not be enough for the code to check all the module files it is monitoring, but for those people configuring it through config or environment variables would probably suffice. I'd be happy to revert the CLI changes and just keeping the |
This is an unfortunate consequence of there already being a lot of options on
I didn't look at your implementation yet, but ideally the delay should add to the time it takes to do the scan (i.e. sleep for that duration between scans): if scanning takes 1 second and the setting is 3 seconds, it should run the scan every four seconds. As you point out, this adapts to different systems better and avoids ending up in a busy loop (or worse, multiple scans running at the same time) if the scanning actually takes longer than the setting is. As has been mentioned before, ideally there would also be an implementation to watch for modification rather than poll for them, if the OS and the filesystem support watching (not always the case even on Linux with network filesystems that may not watch for remote changes).
I like the shorter names the better. Less writing is always good, especially on command line where you need the I should also clarify that I intend to use no authority on this matter, just weighing in on my personal opinion. @ahopkins usually has a good idea of development and knows Sanic better than anyone, so I would like to hear what he thinks too. |
I am going to leave this for now and we can come back to making a better decision on this for LTS. In #2595 I implemented the original strategy so we can release a patch as intended and then finish this. |
So, I am thinking that maybe this should just be a class variable. We have a few different values like this that are really low-level and would cloud the config space. Yet on the other hand we want to maintain some ability to control without ugly patches. Therefore, I propose someone that wants to override do the following: from sanic.worker.reloader import Reloader
Reloader.INTERVAL = 5.0 |
Closing in favor of #2633 |
Fixes #2566
The Reloader class already has an unused
interval
property, this pull request simply adds the missing sleep.