Skip to content

[hmr] Add an API to mark a function as blocking? #494

@CNSeniorious000

Description

@CNSeniorious000

Currently integrations are usually based on external scripts that "import some value from the entry module" and use that for some blocking serving, like

> mcp-hmr script.py:app

and

> uvicorn-hmr main:app

But there exist cases like promplate/hmr#42 where frameworks encourage people to invoke the blocking main loop inside the entry file.

Integrating every framework is too annoying. But forcing downstream users to learn how to use HMR_CONTEXT.effect is also not polite.

The point is, if the entry module blocks, we will never have a chance to start the reload process or even call the on_dispose hook because the ReactiveModule.__load is still running!

I’m wondering if we could add an API like on_dispose -- maybe called mark_block or mark_as_main_loop, which works like

def mark_as_main_loop[T: Callable](func: T) -> T:
    @wraps(func)  # maybe we need to mark it as an async function through `inspect` if it is
    def wrapper(*args, **kwargs):
        __stop_tracking()
        __start_the_reloader_daemon()  # we may need to warn users if they didn't set an `on_dispose` hook here
        return func(*args, **kwargs)

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions