Tilebox offer a standard synchronous API by default, but also give you to option of an async client if you need it.

The synchronous datasets client is great for data exploration in interactive environments like Jupyter notebooks. The asynchronous datasets client is great for building production ready applications that need to scale.

Async is a concurrency model that is far more efficient than multi-threading, and can provide significant performance benefits.

Switching to an async datasets client

Typically all you need to do is swap out your import statement of the Client and you’re good to go. Check out the example below to see how that is done works.

Once you have switched to the async client, you can use the async and await keywords to make your code async. Check out the examples below to see how that works for a few examples.

Async concurrency is also supported in Jupyter notebooks or similar interactive environments. You can even use await some_async_call() as the output of a code cell.

Benefits

The main benefit of using an async client is that you can run requests concurrently, which improve performance. This is especially useful when you are loading data from different collections. Check out the example below to see how that works.

Example: Fetching data concurrently

The following example fetches data from different collections. In the synchronous example, it fetches the data sequentially, whereas in the async example it fetches the data concurrently. This means that the async approach is faster for such use cases.

The output is shown below. As you can see, the async approach is 5 seconds faster. If you have show_progress enabled, the progress bars are updated concurrently. In this example the second collection contains less data than the first one, so it finishes first.

Supported async environments

The Tilebox Datasets Python client supports either asyncio or trio as an async backend. It auto-detects which of those two to use.

AsyncIO

AsyncIO is Python’s built-in library for writing concurrent code with the async/await syntax.

import asyncio
from tilebox.datasets.aio import Client

async def main():
    client = Client()
    datasets = await client.datasets()
    print(datasets)

asyncio.run(main())

Trio

Trio is an alternative async library, designed around the principles of structured concurrency.

import trio
from tilebox.datasets.aio import Client

async def main():
    client = Client()
    datasets = await client.datasets()
    print(datasets)

trio.run(main)

AnyIO

AnyIO is an asynchronous networking and concurrency library that works on top of either asyncio or trio. The Tilebox Datasets Python client is written using anyio, that way it can be used with either asyncio or trio.

import anyio
from tilebox.datasets.aio import Client

async def main():
    client = Client()
    datasets = await client.datasets()
    print(datasets)

anyio.run(main, backend="asyncio")