Under Construction
This site is currently under construction. This page is generated by AI and has not undergone thorough review by a human. There may be hallucination!
In fact, for now only /, /reactive, and the llms.txt are carefully crafted by myself.
What is HMR¶
HMR (Hot Module Reload / Hot Module Replacement) updates running Python code when source files change, without restarting the whole process. Only the changed module and the code that depends on it are re-executed, preserving in-memory state and speeding up your edit-test loop.
Why HMR?¶
Traditional reload (like uvicorn --reload or watchfiles) restarts the entire process on every change. This means:
- Heavy objects (DB clients, ML models) need to be reinitialized every time
- All modules are reimported, even unchanged ones
- Startup time compounds with project size
HMR reload is smarter:
- Only changed modules and their dependents are re-executed
- Unaffected code and state stay untouched
- Runtime dependency tracking ensures accurate invalidation
This makes development much faster, especially for large projects with expensive initialization.
How It Works¶
- File watching: Detects when a source file changes
- Dependency tracking: Uses runtime reactive tracking to know what depends on what
- Selective reload: Re-executes only the changed module and its dependents
- State preservation: Unaffected modules and objects remain in memory
Unlike static analysis tools, HMR tracks dependencies at runtime using a reactive system. This gives fine-grained, name-level precision.
When to Use HMR¶
Good fits:
- Web development (FastAPI, Flask, Starlette)
- MCP server development
- Long-running scripts with expensive setup
- Iterative data analysis with heavy models
When to restart instead:
- Native extensions or C-level state changes
- Protocol-breaking changes in APIs
- When you need guaranteed clean state
Three Ways to Use HMR¶
1. As a CLI (drop-in replacement for python)
2. Framework integrations (dedicated packages)
3. As a library (reactive primitives)
from reactivity import signal, derived, effect
count = signal(0)
@derived
def doubled():
return count.get() * 2
@effect
def logger():
print(f"Doubled: {doubled()}")
Ecosystem¶
| Package | Use Case |
|---|---|
hmr |
Reactive programming lib and hot reloading |
uvicorn-hmr |
ASGI integration (FastAPI, etc.) |
mcp-hmr |
MCP server integration |
fastapi-reloader |
A plugin for uvicorn-hmr to auto-refresh the browser |
hmr-daemon |
Keep variables up-to-date in a daemon thread |
Next Steps¶
- Installation: Install HMR
- Quick Start: Try a simple example