Skip to content

pyflightstream.run.collect (stable)

Generated from the docstrings at build time: every one of the 9 names in the __all__ of pyflightstream.run.collect has an entry below. The names a guide or an example uses come first; the others follow under Advanced.

Collect a submitted job's outputs when they land, then post (FR-99).

THIS IS COLLECT-AND-POST, not submit-and-collect. A watcher stands by, sees that the file is not there yet, waits until it is, and only then generates the post-processing.

WHY IT WATCHES FILES AND NOT THE SCHEDULER: it DECOUPLES THE STAGE FROM THE QUEUE. A watcher that watches FILES needs no status command in the submission profile, no job-script template and no second scheduler vocabulary, and the same stage then serves a cluster job, a local run somebody interrupted, and outputs a colleague dropped in by hand. The collector is a client of the WORKSPACE, which is the thing this package actually owns, rather than of a queue, which it does not.

THE HAZARD THAT DESIGN INHERITS, and it is not hypothetical: A FILE EXISTS BEFORE IT IS FINISHED. The solver opens each export and writes into it, so "the file appeared" is not "the file is complete", and a watcher that fired on appearance alone would post-process a half-written loads table and report numbers for it. That is the same class as every other silent wrong number this package has paid for.

SO THE CONDITION IS PRESENCE AND SETTLED, and settled is asserted by two signals that fail differently:

  1. SIZE AND MODIFICATION TIME STABLE across two observations separated by the poll interval. This costs nothing and catches a file still being written. It is a heuristic with a timer on it: a solver that pauses for longer than the interval between two writes to one file looks finished for one poll. 2b. OR THE SCHEDULER'S OWN LOG HAS BEEN COPIED TO THE DECLARED NAME, which is what happens on a machine whose HPC profile states export_log = false and names a native_log. There EXPORT_LOG is not emitted at all, so the argument below does not hold: the copy is made BEFORE the settle condition is evaluated, and what settles is the copy.

  2. THE LAST DECLARED OUTPUT IS PRESENT. This is the stronger statement, because the emitted script's own order ends with the log: EXPORT_LOG and CLOSE_FLIGHTSTREAM are the last two commands every workflow emits, so a log that exists and has settled means the solver closed the file and left.

Neither is a scheduler query, so neither costs a profile key, which is the whole advantage of this architecture. Both are required, because a run that never wrote its log and a run still writing its loads table are different failures and one signal cannot tell them apart.

ONE-SHOT IS THE PRIMITIVE AND THE WATCH IS A LOOP AROUND IT. Building the watch first would make the feature untestable without a scheduler and unusable from anything that schedules its own polling, such as a cron or a login-node session someone detaches.

NO NINTH STATUS. A job the scheduler killed, or one whose outputs never arrive, takes a FAILED value with the scheduler and the descriptor named. the package already spent a status value on SUBMITTED and the board carries a node asking whether a seventh terminal status was worth amending a published vocabulary for; the answer to that question must not be that so was a ninth. A non-failure outcome is RAN_MISSING_LOG (FR-413): every output of the point is present and its declared solver log is not, a run that ENDED with its outputs and whose log was deleted or never copied back. It is decided by :func:pyflightstream.workspace._missing_log.point_absent_logs before any rule that waits for the log or reads the job's end files, so such a point is neither waited for forever nor failed as a job that ended without its log.

Advanced

pyflightstream.run.collect.CollectOutcome dataclass

CollectOutcome(
    run_id: str,
    state: str,
    detail: str,
    record: RunRecord | AdditionalRecord | None = None,
)

What one submitted point did when the collector looked at it.

pyflightstream.run.collect.CollectReport dataclass

CollectReport(
    collected: list[CollectOutcome] = list(),
    waiting: list[CollectOutcome] = list(),
    failed: list[CollectOutcome] = list(),
    unknown: list[CollectOutcome] = list(),
    discarded: list[RunRecord] = list(),
)

What one sweep did, per point and in total.

outstanding property

outstanding: int

How many submitted points this sweep left waiting FOR SOMETHING.

A point whose record declares no outputs is not counted: nothing here knows what to wait for it, so waiting longer cannot change the answer, and a stop condition that includes it is a stop condition that cannot be reached.

lines

lines() -> list[str]

One human line per point, in the order a reader wants them.

failed_lines

failed_lines() -> list[str]

Return the line :meth:lines prints for each point the sweep failed, and no other.

pyflightstream.run.collect.Stamp dataclass

Stamp(size: int, mtime_ns: int)

What one file looked like at one moment: its size and its mtime.

pyflightstream.run.collect.assess_collected

assess_collected(
    record: RunRecord, sim_dir: Path
) -> tuple[RunStatus, str | None]

Judge a collected point from the files on disk, as a local run is judged.

The default assessor judges settled points from their evidence; a settled process alone does not establish convergence.

IT IS THE SAME JUDGEMENT THE LOCAL PATH MAKES, through the same class, so a point that ran here and a point that ran on a cluster are judged by one rule rather than by two that can drift. :class:LoadsAssessor reads only the collected files and the requested point; it never touches the execution result, which is why the two paths CAN share it even though only one of them has a process to report on.

IT ANSWERS WITH THE PAIR THE INTERFACE DEFINES. Everything the log said -- the iteration, the residual, the times, the file -- is on the :class:~pyflightstream.run.Assessment that :func:assessment_of_collected returns, which is what the collect stage itself reads and stamps.

WHAT IT STILL CANNOT SAY, and the caller is not told otherwise: whether the job was killed before it finished writing is the settle predicate's question, answered before this runs, and a scheduler's own exit status is not visible to a stage that deliberately watches the workspace instead of the queue.

Parameters:

Name Type Description Default
record RunRecord

The SUBMITTED record of the point.

required
sim_dir Path

The point's simulation folder, where the collected files are.

required

Returns:

Type Description
tuple of (RunStatus, str or None)

The point's status and the error that explains it, None when there is none.

pyflightstream.run.collect.assessment_of_collected

assessment_of_collected(
    record: RunRecord, sim_dir: Path
) -> Assessment

Return the WHOLE judgement of a collected point, not only its verdict.

:func:assess_collected is the assessor INTERFACE a caller may replace, and it answers with the (status, error) pair that interface defines. This one answers with the :class:~pyflightstream.run.Assessment behind it: the iteration it reached, the residual it reached it at, the times the solver printed, and which file they were read from. On a cluster that is the whole of the evidence, because there is no process here to report on -- the record was the only place those numbers could land, and in legacy releases they landed nowhere.

Parameters:

Name Type Description Default
record RunRecord

The SUBMITTED record of the point.

required
sim_dir Path

The point's simulation folder, where the collected files are.

required

Returns:

Type Description
Assessment

The status, the error and the evidence read from the log.

pyflightstream.run.collect.collect_and_post

collect_and_post(
    workspace: CampaignWorkspace,
    *,
    watch: bool = False,
    discard_walltime: bool = False,
    interval: float = DEFAULT_SETTLE_INTERVAL_S,
    watch_interval: float = DEFAULT_WATCH_INTERVAL_S,
    rounds: int | None = None,
    sleep: Callable[[float], None] = time.sleep,
    post: _Post | None = None,
    post_matrix: _PostMatrix | None = None,
    observer: Callable[
        [Iterable[Path]], dict[str, Stamp | None]
    ] = observe,
    assessor: Callable[
        [RunRecord, Path], tuple[RunStatus, str | None]
    ]
    | None = None,
    sims: Collection[str] | None = None,
) -> CollectReport

Collect, then post-process after collection or an explicit walltime discard.

THE LOOP AROUND THE PRIMITIVE. With watch false this is one sweep, which is what a cron wants. With watch true it sweeps until nothing is outstanding, or until rounds sweeps have run, which is the bound that stops a test or a mistake becoming an endless loop.

THE POST RUNS ONLY WHERE SOMETHING CHANGED. A sweep that found nothing new must not rewrite the products: rebuilding can archive the previous ones, so a watch that posted every minute would fill the archive with copies of an unchanged answer.

post is called once per sweep that collected or discarded something, with the workspace alone. post_matrix is called once per MATRIX that sweep collected or discarded a record of, with the matrix stem the record names (None for a record that names none), in the order first collected. The products of a workspace are rebuilt per matrix, so a caller that rebuilds them needs the second: the command line's own post called the stage with no matrix, and the stage then selected the records naming none, which left every named-matrix record out and wrote nothing.

sims (FR-307) limits every sweep to the SUBMITTED records of those simulations (:func:collect_once) and the post to the same simulations: post is then called with the keyword sims too, and post_matrix with sims naming the ones of that matrix. Without sims, post_matrix is called with sims naming the simulations of that matrix the sweep collected or discarded, so every other simulation keeps its products (PFS-2185.18).

Parameters:

Name Type Description Default
workspace CampaignWorkspace

The campaign workspace whose manifest is swept.

required
watch bool

Sweep until nothing is outstanding or rounds sweeps have run; one sweep when false.

False
discard_walltime bool

Mark the latest WALLTIME_REACHED records in scope FAILED_MARKED after each sweep, before post. Keep their outputs for the next run to archive.

False
interval float

Seconds between the two observations that decide settled.

DEFAULT_SETTLE_INTERVAL_S
watch_interval float

Seconds between two sweeps of a watch.

DEFAULT_WATCH_INTERVAL_S
rounds int

The most sweeps a watch runs; None for no bound.

None
sleep callable

The clock, injected so a test runs with none.

sleep
post callable

Called with the workspace once per sweep that collected or discarded something, and with the keyword sims too when sims scopes the collect; it must take it.

None
post_matrix callable

Called once per matrix a sweep changed a record of, with the workspace and the matrix stem, and with the keyword sims too when sims scopes the collect; it must take it.

None
observer callable

Stamps the paths, :func:observe by default.

observe
assessor callable

Judges a collected point. None, the default, and :func:assess_collected passed explicitly are the package's own assessor, and everything it read from the log is stamped on the record. Any other callable answers (status, error) only, and no log field is stamped.

None
sims collection of str

Limit every sweep and the post to the SUBMITTED records of these simulations.

None

Returns:

Type Description
CollectReport

The points every sweep collected, failed and discarded, and the points the last sweep left waiting or could not wait for.

pyflightstream.run.collect.collect_once

collect_once(
    workspace: CampaignWorkspace,
    *,
    interval: float = DEFAULT_SETTLE_INTERVAL_S,
    sleep: Callable[[float], None] = time.sleep,
    observer: Callable[
        [Iterable[Path]], dict[str, Stamp | None]
    ] = observe,
    assessor: Callable[
        [RunRecord, Path], tuple[RunStatus, str | None]
    ]
    | None = None,
    sims: Collection[str] | None = None,
    writes: CollectWrites | None = None,
) -> CollectReport

Sweep every SUBMITTED record once and collect the ones that settled.

THE PRIMITIVE. It returns rather than waits, so a caller can run it from a cron, from a login-node session, or in the loop :func:collect_and_post puts around it.

Parameters:

Name Type Description Default
workspace CampaignWorkspace

The campaign workspace whose manifest is swept.

required
interval float

Seconds between the two observations that decide "settled".

DEFAULT_SETTLE_INTERVAL_S
sleep Callable[[float], None]

(assessor as in :func:collect_and_post.) Injected so a test can run this with no clock, no filesystem delay and no solver. A stage whose only entry point needs a scheduler is a stage nothing tests, which is the reason the injection exists rather than a preference for injection.

sleep
observer Callable[[float], None]

(assessor as in :func:collect_and_post.) Injected so a test can run this with no clock, no filesystem delay and no solver. A stage whose only entry point needs a scheduler is a stage nothing tests, which is the reason the injection exists rather than a preference for injection.

sleep
assessor Callable[[float], None]

(assessor as in :func:collect_and_post.) Injected so a test can run this with no clock, no filesystem delay and no solver. A stage whose only entry point needs a scheduler is a stage nothing tests, which is the reason the injection exists rather than a preference for injection.

sleep
sims Collection[str] | None

FR-307, collect --sims: sweep only the SUBMITTED records of these simulations; every other record is left untouched and is not counted as outstanding. A simulation no record holds is refused by name before anything is swept. None sweeps every record.

None
writes CollectWrites | None

Filesystem writes, grouped preparation and extraction completion, as :class:CollectWrites; None makes the real writes. A read-only collection passes refusals.

None

Returns:

Type Description
CollectReport

The points the sweep collected, failed, left waiting, and could not wait for because their record declares no outputs.

Raises:

Type Description
WorkspaceError

If the manifest cannot be read, or sims names a simulation no record holds.

pyflightstream.run.collect.observe

observe(paths: Iterable[Path]) -> dict[str, Stamp | None]

Return one stamp per path, or None where the path does not exist yet.

NONE IS A REAL ANSWER HERE and not an error: the whole point of this stage is that it runs while the files are still missing.

Parameters:

Name Type Description Default
paths iterable of Path

The files to stamp.

required

Returns:

Type Description
dict of str to Stamp or None

Path to its size and modification time, or None where it does not exist.

pyflightstream.run.collect.settled

settled(
    before: Mapping[str, Stamp | None],
    after: Mapping[str, Stamp | None],
) -> bool

Return True when every path is present in both observations and unchanged.

A path missing from either observation is NOT settled, which is the branch that carries the "presence" half of the condition; a path whose size or mtime moved between them is still being written.

Parameters:

Name Type Description Default
before mapping of str to Stamp or None

The first observation, from :func:observe.

required
after mapping of str to Stamp or None

The second observation.

required

Returns:

Type Description
bool

True when every path is present in both and unchanged.