Sign in Get started

Async client

The Pictograph Python SDK's asyncio client: every resource and argument of Client, awaited, with concurrent requests on one HTTP/2 connection.

View as Markdown
On this page

AsyncClient has every resource, method and argument of Client: await each call and async for each iter(...). Requests share one HTTP/2 connection, so many run at once without threads. Credentials, retries and errors work as in the sync client.

Source: aio/client.py

Quick start

import asyncio
from pictograph import AsyncClient


async def main() -> None:
    async with AsyncClient(api_key="pk_live_...") as client:
        # await any resource method
        datasets = await client.datasets.list(limit=5)

        # async-for the auto-paging iterators
        async for img in client.images.iter(dataset=datasets[0].name, directory="/train"):
            print(img.filename, img.annotation_count)


asyncio.run(main())

Use async with so the connection closes, or call await client.aclose().

Concurrent requests

import asyncio
from pictograph import AsyncClient


async def main() -> None:
    async with AsyncClient() as client:
        datasets = await client.datasets.list(limit=20)
        # Fetch health insights for every dataset at once.
        reports = await asyncio.gather(
            *(client.datasets.insights(dataset=d.name) for d in datasets)
        )
        for d, health in zip(datasets, reports):
            print(d.name, health.total_annotations)


asyncio.run(main())

Long-running jobs

A call that waits for a job doesn’t block the loop. await it, or pass wait=False and check on the job yourself:

async with AsyncClient() as client:
    export = await client.exports.create(
        dataset="road-signs",
        name="nightly",
        format="coco",
    )

    run = await client.training.create(
        dataset="road-signs",
        export="nightly",
        pipeline_type="yolox",
        name="detector",
        wait=True,
    )
    print(run.status)

Paging

iter(...) returns an async pager:

async with AsyncClient() as client:
    # Collect every page, or read them as they come.
    all_models = await client.models.iter().all()
    async for run in client.training.iter(status="completed"):
        print(run.name)

Local inference runs on your own CPU or GPU, so it has no async form.

Copied to clipboard