Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Autosave

Autosave periodically saves your read position without explicit save() calls. Turn it on in Config to protect against crashes and interruptions

Enabling Autosave

Set auto_save_state to true. You can adjust auto_save_state_bytes to control how often saves happen (default: every 100 MiB). Set auto_load_state to true to resume auto-named states on the next run

Rust

use std::num::NonZeroU64;
use preader::{Config, PReader};

let config = Config {
    state_dir: "/tmp/states".into(),
    auto_save_state: true,
    auto_save_state_bytes: NonZeroU64::new(1024).unwrap(),
    auto_load_state: true,
    ..Config::default()
};
let reader = PReader::from(config);

// First run, read 300 lines, autosave happens automatically
let count = reader.lines("data.txt").build()?.take(300).count();

// Second run, resumes from the last autosave
let mut resumed = reader.lines("data.txt").build()?;
println!("resumes at {:.1}%", resumed.percent());

Python

from itertools import islice
from preader import Config, PReader

config = Config(
    state_dir="/tmp/states",
    auto_save_state=True,
    auto_save_state_bytes=1024,
    auto_load_state=True,
)

reader = PReader(config=config)
first = reader.lines("data.txt")

list(islice(first, 300))
del first  # triggers autosave on drop

resumed = reader.lines("data.txt")
print(f"resumes at {resumed.percent():.1f}%")

When Autosave Triggers

Autosave fires at the following points during an iterator’s lifetime:

  1. Threshold: When the number of bytes read since the last save crosses a multiple of auto_save_state_bytes
  2. Finalize: When the iterator is fully consumed
  3. Drop: When the iterator goes out of scope or is explicitly deleted

At each trigger, preader checks whether any progress was made since the last save and skips the write if nothing changed

Failure Behavior

If an autosave fails during iteration, the error is returned as Err(...) in Rust or raised as an exception in Python, and the iterator stops. If the failure happens during drop, the error is printed to stderr as "preader: save failed: ..."

Auto-Load Behavior

auto_load_state only affects auto-named states (when state is None or omitted). Named states like state="job-1" are always loaded if they exist, regardless of this setting

When auto_load_state is false (the default), omitting the state name always starts a fresh read even if an auto-named state exists on disk