uvicorn-hmr
This package provides hot module reloading (HMR) for uvicorn.
It uses watchfiles to detect FS modifications,
re-executes the corresponding modules with hmr and restart the server (in the same process).
HOT means the main process never restarts, and reloads are fine-grained (only the changed modules and their dependent modules are reloaded).
Since the python module reloading is on-demand and the server is not restarted on every save, it is much faster than the built-in --reload option provided by uvicorn.
Why?
- When you use
uvicorn --reload, it restarts the whole process on every file change, but restarting the whole process is unnecessary:- There is no need to restart the Python interpreter, neither all the 3rd-party packages you imported.
- Your changes usually affect only one single file, the rest of your application remains unchanged.
hmrtracks dependencies at runtime, remembers the relationships between your modules and only reruns necessary modules.- Since v0.7,
hmralso tracks file system access - any file your code reads (config files, templates, data files, etc.) becomes reactive and will trigger reloads when modified. - So you can save a lot of time by not restarting the whole process on every file change. You can see a significant speedup for debugging large applications.
- Although magic is involved, we thought and tested them very carefully, so everything works just as-wished.
- Your lazy loading through module-level
__getattr__still works - Your runtime imports through
importlib.import_moduleor even__import__still work - Even valid circular imports between
__init__.pyand sibling modules still work - Fine-grained dependency tracking in the above cases still work
- Decorators still work, even meta programming hacks like
getsourcecalls work too - Standard dunder metadata like
__name__,__doc__,__file__,__package__are correctly set - ASGI lifecycles are preserved
- Your lazy loading through module-level
- We did some elegant engineering to optimize UX and performance of
uvicorn:- Since the reloader and the server stay in the same thread, we can gracefully wait for the server to shutdown before reloading, which avoids long weird tracebacks that you will see when you keep pressing save during
uvicorn --reload. uvicorn's main loop has a 100ms tick interval, we optimized it into an event-driven model, so the server can respond to shutdown calls instantly!
- Since the reloader and the server stay in the same thread, we can gracefully wait for the server to shutdown before reloading, which avoids long weird tracebacks that you will see when you keep pressing save during
Normally, you can replace uvicorn --reload with uvicorn-hmr and everything will work as expected, with a much faster refresh experience.
Installation
pip install uvicorn-hmr
Or with extra dependencies:
pip install uvicorn-hmr[all]
This will also install fastapi-reloader, enabling the --refresh flag for automatic browser page refresh.
About the auto refresher
The
--refreshflag enables automatic HTML page refresh via thefastapi-reloaderpackage. This differs from Uvicorn's built-in--reloadfunctionality (see configuration section for details).Server reloading is a core feature of
uvicorn-hmrand is always active, regardless of the--refreshflag. The--refreshflag specifically controls HTML page auto-refresh, a feature not available in standard Uvicorn.If you don't need HTML page auto-refresh, simply omit the
--refreshflag. If you do, ensurefastapi-reloaderis installed viapip install fastapi-reloaderorpip install uvicorn-hmr[all].
Usage
Replace
uvicorn main:app --reload
with
uvicorn-hmr main:app
Everything will work as-expected, but with hot module reloading.
CLI Arguments
I haven't copied all the configurable options from uvicorn. But contributions are welcome!
For now, host, port, log-level, env-file are supported and have exactly the same semantics and types as in uvicorn.
The behavior of reload_include and reload_exclude is different from uvicorn in several ways:
- Uvicorn allows specifying patterns (such as
*.py), but in uvicorn-hmr only file or directory paths are allowed; patterns will be treated as literal paths. - Uvicorn supports watching non-Python files (such as templates), and uvicorn-hmr also supports hot-reloading when any accessed files are modified (including config files, templates, data files, etc.) through reactive file system tracking.
- Uvicorn always includes/excludes all Python files by default (even if you specify
reload-includeorreload-exclude, all Python files are still watched/excluded accordingly), but uvicorn-hmr only includes/excludes the paths you specify. If you do not providereload_include, the current directory is included by default; if you do provide it, only the specified paths are included. The same applies toreload_exclude.
The following options are supported but do not have any alternative in uvicorn:
--refresh: Enables auto-refreshing of HTML pages in the browser whenever the server restarts. Useful for demo purposes and visual debugging. This is totally different fromuvicorn's built-in--reloadoption, which is always enabled and can't be disabled inuvicorn-hmrbecause hot-reloading is the core feature of this package.--clear: Wipes the terminal before each reload. Just likevitedoes by default.
The two features above are opinionated and are disabled by default. They are just my personal practices. If you find them useful or want to suggest some other features, feel free to open an issue.
Metadata
Release files for uvicorn-hmr 0.1.1.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| uvicorn_hmr-0.1.1.8-py3-none-any.whl | Python 3 | none | any | Details |
Release files / uvicorn_hmr-0.1.1.8-py3-none-any.whl
| Download URL | uvicorn_hmr-0.1.1.8-py3-none-any.whl |
|---|---|
| Size | 7.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7c789e1eccd62ca7f2af22b7f6989eebcf8e8c2acd330b21918f9b530a204e57
|
|
BLAKE2b-256 checksum How to use checksums |
ba0335e9888dd5b88283013559161403701ff72927c34de8a21b41e196d355d4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.9.8
|
Security for uvicorn-hmr 0.1.1.8
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.