Legacy embedding API
This page describes the API shipped in hmr==0.7.6.3. It is a low-level integration surface, not a promise about a future replacement or release date. Prefer the CLI or a framework integration when possible.
Running a reloader in an existing program
Create the reloader before importing application modules that should be reactive. Run from the application directory so the default includes=(".",) covers those modules.
from reactivity.hmr.api import SyncReloaderAPI
with SyncReloaderAPI("app.py"):
input("Edit app.py, then press Enter to stop.\n")
The entry file runs when the context is entered, before the watcher starts. It must return control; an indefinitely blocking server loop at module scope prevents startup from completing. The synchronous context runs the watcher in a background thread and joins it on exit. AsyncReloaderAPI also supports synchronous and asynchronous contexts; an asynchronous context awaits the watcher task during shutdown.
Keep a reloader variable separately if needed. Do not rely on the value of with ... as value: these legacy context managers return a super() proxy, not the reloader instance.
Cleanup is not a process reset
Context exit stops the watcher, disposes the entry effect/loading computation, and restores sys.modules["__main__"]. In 0.7.6.3, it does not remove the installed import finder, unload imported modules, or run all application disposal callbacks. Own application resources with an explicit try/finally or context manager. Avoid repeatedly creating reloaders as a substitute for process isolation.
from reactivity.hmr.api import *
LifecycleMixin
Bases: BaseReloader
Source code in reactivity/hmr/api.py
7
8
9
10
11
12
13
14
15
16
17
18
19
|
class LifecycleMixin(BaseReloader):
def run_with_hooks(self):
self._original_main_module = sys.modules["__main__"]
sys.modules["__main__"] = self.entry_module
call_pre_reload_hooks()
self.effect = HMR_CONTEXT.effect(self.run_entry_file)
call_post_reload_hooks()
def clean_up(self):
self.effect.dispose()
self.entry_module.load.dispose()
self.entry_module.load.invalidate()
sys.modules["__main__"] = self._original_main_module
|
run_with_hooks
Source code in reactivity/hmr/api.py
|
|
def run_with_hooks(self):
self._original_main_module = sys.modules["__main__"]
sys.modules["__main__"] = self.entry_module
call_pre_reload_hooks()
self.effect = HMR_CONTEXT.effect(self.run_entry_file)
call_post_reload_hooks()
|
clean_up
Source code in reactivity/hmr/api.py
|
|
def clean_up(self):
self.effect.dispose()
self.entry_module.load.dispose()
self.entry_module.load.invalidate()
sys.modules["__main__"] = self._original_main_module
|
SyncReloaderAPI
Bases: SyncReloader, LifecycleMixin
Source code in reactivity/hmr/api.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
class SyncReloaderAPI(SyncReloader, LifecycleMixin):
def __enter__(self):
from threading import Thread
self.run_with_hooks()
self.thread = Thread(target=self.start_watching)
self.thread.start()
return super()
def __exit__(self, *_):
self.stop_watching()
self.thread.join()
self.clean_up()
async def __aenter__(self):
from asyncio import ensure_future, sleep, to_thread
await to_thread(self.run_with_hooks)
self.future = ensure_future(to_thread(self.start_watching))
await sleep(0)
return super()
async def __aexit__(self, *_):
self.stop_watching()
await self.future
self.clean_up()
|
__enter__
Source code in reactivity/hmr/api.py
|
|
def __enter__(self):
from threading import Thread
self.run_with_hooks()
self.thread = Thread(target=self.start_watching)
self.thread.start()
return super()
|
__exit__
Source code in reactivity/hmr/api.py
|
|
def __exit__(self, *_):
self.stop_watching()
self.thread.join()
self.clean_up()
|
__aenter__
async
Source code in reactivity/hmr/api.py
|
|
async def __aenter__(self):
from asyncio import ensure_future, sleep, to_thread
await to_thread(self.run_with_hooks)
self.future = ensure_future(to_thread(self.start_watching))
await sleep(0)
return super()
|
__aexit__
async
Source code in reactivity/hmr/api.py
|
|
async def __aexit__(self, *_):
self.stop_watching()
await self.future
self.clean_up()
|
AsyncReloaderAPI
Bases: AsyncReloader, LifecycleMixin
Source code in reactivity/hmr/api.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
class AsyncReloaderAPI(AsyncReloader, LifecycleMixin):
def __enter__(self):
from asyncio import run
from threading import Event, Thread
self.run_with_hooks()
e = Event()
async def task():
e.set()
await self.start_watching()
self.thread = Thread(target=lambda: run(task()))
self.thread.start()
e.wait()
return super()
def __exit__(self, *_):
self.stop_watching()
self.thread.join()
self.clean_up()
async def __aenter__(self):
from asyncio import ensure_future, sleep, to_thread
await to_thread(self.run_with_hooks)
self.future = ensure_future(self.start_watching())
await sleep(0)
return super()
async def __aexit__(self, *_):
self.stop_watching()
await self.future
self.clean_up()
|
__enter__
Source code in reactivity/hmr/api.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
def __enter__(self):
from asyncio import run
from threading import Event, Thread
self.run_with_hooks()
e = Event()
async def task():
e.set()
await self.start_watching()
self.thread = Thread(target=lambda: run(task()))
self.thread.start()
e.wait()
return super()
|
__exit__
Source code in reactivity/hmr/api.py
|
|
def __exit__(self, *_):
self.stop_watching()
self.thread.join()
self.clean_up()
|
__aenter__
async
Source code in reactivity/hmr/api.py
|
|
async def __aenter__(self):
from asyncio import ensure_future, sleep, to_thread
await to_thread(self.run_with_hooks)
self.future = ensure_future(self.start_watching())
await sleep(0)
return super()
|
__aexit__
async
Source code in reactivity/hmr/api.py
|
|
async def __aexit__(self, *_):
self.stop_watching()
await self.future
self.clean_up()
|