Asyncio integration

koskari.asyncio

Python asyncio support for Koskari. Primarily focuses on compatability with asyncio loops which specifically check for the Python coroutine type.

class koskari.asyncio.ProcessResult(args, returncode, stdout, stderr)

Bases: object

The captured outcome of a completed subprocess.

Bundles the argv, exit status, and raw stdout/stderr of a finished process in the manner of an HTTP client’s Response object: capture everything, interpret afterwards. Helpers decode and parse the captured stdout; raise_for_status opts into strict exit-status handling.

json()

The captured stdout parsed as a single JSON document

jsonl()

The captured stdout parsed as JSON Lines: a list with one parsed document per non-blank line

raise_for_status()

Raise CalledProcessError when the process exited nonzero.

Returns self otherwise, so strict callers can chain, e.g. (.json (.raise_for_status result))

property stderr_text

The captured stderr, decoded as utf-8

property text

The captured stdout, decoded as utf-8

yaml()

The captured stdout parsed as a single YAML document. Requires the optional PyYAML package

async koskari.asyncio.as_coroutine(awaitable)

Convert an awaitable into a Python coroutine function. This is a compatibility layer for asyncio loops which specifically check for the coroutine type.

koskari.asyncio.coroutine_fn(fn)

Convert an async function into a Python coroutine function. This is a compatibility layer for asyncio loops which specifically check for the coroutine type.

koskari.asyncio.create_task(coro)

Create and schedule a Task from an awaitable, returning the Task itself. Tasks are handles: they are excluded from implicit auto-await, support cancellation and done-callbacks, and resolve under explicit await.

Python coroutines, Koskari Coroutines, and defers all pass asyncio.iscoroutine and schedule directly; exotic non-coroutine awaitables are adapted through as_coroutine first. Note that passing a defer consumes it – the Task owns its single shot.

async koskari.asyncio.run_process(*args)

Run an argv to completion in a subprocess, returning a ProcessResult

Launches the argv with asyncio, captures stdout and stderr, and waits for the process to exit. The exit status is reported on the result, never raised; use ProcessResult.raise_for_status for strict handling.

koskari.asyncio.run_until_complete(loop, coro)

Run a coroutine until completion. Will automatically convert a defer or Koskari Coroutine into a Python coroutine object. Blocks until the coroutine completes and returns the result or raises an exception.

Parameters:
  • loop – The event loop to use. If None, the coroutine runs on a fresh loop through asyncio.run, closed when it returns.

  • coro – The coroutine to run.

Returns:

The result of the coroutine.