Why use async?

When working with external datasets, such as Tilebox datasets, loading data may take some time. To speed up this process, you can run requests in parallel. While you can use multi-threading or multi-processing, which can be complex, often times a simpler option is to perform data loading tasks asynchronously using coroutines and asyncio.

Switching to an async datasets client

To switch to the async client, change the import statement for the Client. The example below illustrates this change.

After switching to the async client, use await for operations that interact with the Tilebox API.

Jupyter notebooks and similar interactive environments support asynchronous code execution. You can use await some_async_call() as the output of a code cell.

Fetching data concurrently

The primary benefit of the async client is that it allows concurrent requests, enhancing performance. In below example, data is fetched from multiple collections. The synchronous approach retrieves data sequentially, while the async approach does so concurrently, resulting in faster execution.

The output demonstrates that the async approach runs approximately 30% faster for this example. With show_progress enabled, the progress bars update concurrently.

Async workflows

The Tilebox workflows Python client does not have an async client. This is because workflows are designed for distributed and concurrent execution outside a single async event loop. But within a single task, you may use still useasync code to take advantage of asynchronous execution, such as parallel data loading. You can achieve this by wrapping your async code in asyncio.run.

Below is an example of using async code within a workflow task.

If you encounter an error like RuntimeError: asyncio.run() cannot be called from a running event loop, it means you’re trying to start another asyncio event loop (with asyncio.run) from within an existing one. This often happens in Jupyter notebooks since they automatically start an event loop. A way to resolve this is by using nest-asyncio.