Skip to content

ontocast.api.batch

Run a request's documents one after another, each to its own result.

Attributes

RunDocument = Callable[[AgentState], Awaitable[ProcessOkResponse | ProcessErrorResponse]] module-attribute

logger = logging.getLogger(__name__) module-attribute

Classes

Functions:

run_documents(states, run_one, semaphore) async

Run run_one on each state in order; one document's failure stops nothing.

Each run holds semaphore on its own, so a long request does not keep a slot between its documents.

Parameters:

Name Type Description Default
states Sequence[AgentState]

One state per document, each with input_document set.

required
run_one RunDocument

Extracts one document; returns its result or its error.

required
semaphore Semaphore | None

The server's concurrency cap, if any.

required

Returns:

Name Type Description
ProcessBatchResponse ProcessBatchResponse

One result per state, in order.

Source code in ontocast/api/batch.py
async def run_documents(
    states: Sequence[AgentState],
    run_one: RunDocument,
    semaphore: asyncio.Semaphore | None,
) -> ProcessBatchResponse:
    """Run ``run_one`` on each state in order; one document's failure stops nothing.

    Each run holds ``semaphore`` on its own, so a long request does not keep a
    slot between its documents.

    Args:
        states: One state per document, each with ``input_document`` set.
        run_one: Extracts one document; returns its result or its error.
        semaphore: The server's concurrency cap, if any.

    Returns:
        ProcessBatchResponse: One result per state, in order.
    """
    results: list[ProcessItemResult] = []
    for state in states:
        if semaphore is None:
            results.append(await _run(state, run_one))
            continue
        async with semaphore:
            results.append(await _run(state, run_one))
    return ProcessBatchResponse(results=results)