spark.worker.checkpoint
function checkpoint(): Promise<void>Cooperative checkpoint that enables pause/stop/reload from the UI.
Call this frequently in your training loop to allow the UI to pause, stop, or request hot reload without losing state. Checkpoints are very lightweight when nothing is requested (minimal overhead).
Checkpoints provide:
- Pause support: Execution pauses until UI clicks resume
- Stop support: Throws SparkStopError if UI clicked stop
- Reload support: Throws SparkReloadError if code reload requested
- UI yield: Small timeout allowing UI to process events
Call this in inner training loops (per batch) for responsive UI.
Checkpoints should be called frequently (every batch or every N batches) for responsive UI. They have minimal overhead when pause/stop not active.
Returns
Promise<void>– Promise that resolves when checkpoint completes, or throws if stopped/reloadExamples
// In your training loop
async function train() {
for (let epoch = 0; epoch < 100; epoch++) {
for (const batch of data) {
// Checkpoint allows UI control
try {
await spark.checkpoint();
} catch (err) {
if (err instanceof SparkStopError) {
console.log('Training stopped by user');
return; // Exit cleanly
}
throw err; // Reload errors are handled by spark
}
// Training step
const output = model(batch.x);
const loss = criterion(output, batch.y);
// ...
}
}
}See Also
- createController - Get a controller to check status or manually pause/resume