astrocook.recipes.progress module#

The progress of a running recipe, as the recipes report it in the log.

A long recipe writes >> PROGRESS: n (n from 0 to 100) with logger.info, next to the lines that say what it is doing. The reader of those lines is whoever shows the progress: the GUI (the dialog with its bar, gui/recipe_progress.py), the MCP server (progress notifications to the client, phase 6-ter PR 4) or a script (watch_progress()). The format is read here only, so that the readers cannot differ.

The same places are where a recipe can be cancelled (phase 6-ter, PR 7-quater): whoever runs a recipe in a thread runs it inside cancellable(), with a threading.Event that it sets from another thread to cancel it (the Stop of the GUI, the cancellation of a client of the MCP server, its session_cancel tool); the recipe calls check_cancelled(), or progress(), which also logs the progress line, and stops with RecipeCancelled. The event belongs to the thread, not to the session, so that the sessions stay immutable and the steps of a pipeline (each on a new session) see it.

astrocook.recipes.progress.PROGRESS_MARKER = '>> PROGRESS:'#

What a recipe writes before the percentage

astrocook.recipes.progress.is_progress_line(message: str) → bool[source]#

Whether a log message is a progress line (its value may be unreadable).

astrocook.recipes.progress.parse_progress(message: str) → int | None[source]#

The percentage of a progress line, or None if the message is not one or its value is not an integer.

astrocook.recipes.progress.watch_progress(callback: Callable[[int, str], None], logger_name: str = 'astrocook') → Iterator[None][source]#

Call callback(percent, status) for each progress line that the current thread logs, while the block runs.

The records are INFO: the level of the logger logger_name (the package, by default) is lowered to INFO if it is higher, and restored after, so that the lines reach the handler whatever the application configured for the root logger.

astrocook.recipes.progress.cancellable(event: Event | None = None) → Iterator[Event][source]#

Run the block (a recipe) so that setting event cancels it.

The event (a new one if None) is the one that check_cancelled() reads in the current thread while the block runs; it is yielded, to be set from another thread.

astrocook.recipes.progress.cancel_requested() → bool[source]#

Whether the recipe running in the current thread was cancelled.

astrocook.recipes.progress.check_cancelled() → None[source]#

Stop the recipe running in the current thread if it was cancelled.

Raises:

RecipeCancelled – If the event of cancellable() is set.

astrocook.recipes.progress.progress(logger: Logger, percent: int) → None[source]#

Log the progress line of a recipe, then stop it if it was cancelled.