Skip to content

Async and sync clients

Every client ships two variants. OpenAlexClient runs on asyncio; OpenAlexSyncClient is its sync twin and blocks until the answer arrives. Both take the same parameters, raise the same errors, retry and pace requests the same way, and return the same models.

Pick the right variant

Use the async client inside asyncio applications, such as web services or harvesters that keep many requests in flight. Use the sync client in scripts, notebooks, and code that already runs inside an event loop. The sync twin uses httpx.Client and threads, and never starts an event loop.

The names follow one pattern: the async client is <Api>Client, the sync client is <Api>SyncClient. For example OpenAlexClient and OpenAlexSyncClient, or CrossrefClient and CrossrefSyncClient.

Make the same call in both

import asyncio

from bibliofabric.openalex import OpenAlexClient, OpenAlexSyncClient

DOI = "10.1038/nature12373"


async def main() -> None:
    async with OpenAlexClient() as client:
        work = await client.works.get(DOI)
        print("async:", work.title)


asyncio.run(main())

with OpenAlexSyncClient() as client:
    work = client.works.get(DOI)
    print("sync: ", work.title)
async: Nanometre-scale thermometry in a living cell
sync:  Nanometre-scale thermometry in a living cell

Both variants close their connections through the context manager (async with and with). You can also call the client without a context manager and close it later with await client.aclose() or client.close().

Work in a notebook

A Jupyter cell already runs an event loop, so asyncio.run(main()) in a cell fails with asyncio.run() cannot be called from a running event loop. The sync client works in a cell because it never touches the loop:

from bibliofabric.openalex import OpenAlexSyncClient

with OpenAlexSyncClient() as client:
    work = client.works.get("10.1038/nature12373")
    print(work.title)
Nanometre-scale thermometry in a living cell

In a Jupyter cell you can also await the async client directly, because the cell runs inside the loop. The how-to pages on this site write their snippets in that form. They create a client once and then await calls at the top level. To run such a snippet as a script, wrap it in async def main() and call asyncio.run(main()), as the quick start does.

Use the sync client from threads

One sync client serves several threads. The client paces and retries requests shared across all of them:

from concurrent.futures import ThreadPoolExecutor

from bibliofabric.openalex import OpenAlexSyncClient

DOIS = ["10.1038/nature12373", "10.1109/5.771073"]

with OpenAlexSyncClient() as client:
    with ThreadPoolExecutor(max_workers=2) as pool:
        for work in pool.map(lambda doi: client.works.get(doi), DOIS):
            print(work.doi, "|", work.title)
https://doi.org/10.1038/nature12373 | Nanometre-scale thermometry in a living cell
https://doi.org/10.1109/5.771073 | Toward unique identifiers

The async client reaches the same parallelism with asyncio.gather and the max_concurrency setting instead of threads.

Bring your own httpx client

Pass http_client= to reuse one httpx.AsyncClient (or httpx.Client for the sync twin) across your own code and the bibliofabric client. When you pass your own client, you also own its lifecycle, so the bibliofabric client will not close it:

import asyncio

import httpx

from bibliofabric.openalex import OpenAlexClient


async def main() -> None:
    http = httpx.AsyncClient(
        base_url="https://api.openalex.org",
        timeout=10.0,
        headers={"User-Agent": "my-harvester/1.0"},
    )
    async with OpenAlexClient(http_client=http) as client:
        work = await client.works.get("10.1038/nature12373")
        print(work.title)


asyncio.run(main())
Nanometre-scale thermometry in a living cell