composeui.core.shared

Data loaded once per process and shared by its sessions, read-only (see shared_data).

class composeui.core.shared.SharedData[source]

Bases: Generic[R, ProgressT]

A value loaded once per process, given to each session’s job: see shared_data.

Pass it to Jobs.task or Jobs.start as the job’s function. The first job starts the load in a thread of its own, the jobs that come meanwhile wait for it, and later jobs get the value at once. Each job reports the load’s progress. A job canceled stops waiting and raises JobCanceledError; the load stops once no job waits for it. A failed load is not kept: the next job loads again.

__init__(load)[source]
Parameters:

load (Callable[[Job[R, ProgressT]], R])

Return type:

None

forget()[source]

Drop the value, so that the next job loads it again, e.g. after its data changed.

The sessions keep their copies. A load going on still gives its value to the jobs that wait for it, but the next job loads again.

Return type:

None

exception composeui.core.shared.SharedDataError[source]

Bases: Exception

A shared load returned a value that a session could change, so it isn’t shared.

composeui.core.shared.shared_data(load)[source]

Load load’s value once per process, and share it with every session, read-only.

A decorator, e.g. of a function reading a large CSV, which each session’s job then gets without reading it again. load takes only its job: its value can’t depend on a session’s input, which would share private data. The value is protected once loaded, or refused (SharedDataError) if a session could change it:

  • a Polars frame or series: each session gets a clone, which shares its columns;

  • a pandas frame or series, with pandas’ copy-on-write on: a copy per session;

  • a NumPy array: read-only, with the arrays it’s a view of, but not one over memory that can be written, e.g. a bytearray;

  • a Polars lazy frame, a PyArrow table or chunked array: as it is;

  • an immutable value: a number, a string, a date, None, an enum member, or a tuple, frozenset or frozen attrs class of them.

Parameters:

load (Callable[[Job[R, ProgressT]], R])

Return type:

SharedData[R, ProgressT]