Utilities

aiostream also provides utilites for general asynchronous iteration and asynchronous context management.

Asynchronous iteration

Utilities for asynchronous iteration.

aiostream.aiter_utils.aiter(obj)

Access aiter magic method.

aiostream.aiter_utils.anext(obj)

Access anext magic method.

aiostream.aiter_utils.await_(obj)

Identity coroutine function.

aiostream.aiter_utils.async_(fn)

Wrap the given function into a coroutine function.

aiostream.aiter_utils.is_async_iterable(obj)

Check if the given object is an asynchronous iterable.

aiostream.aiter_utils.assert_async_iterable(obj)

Raise a TypeError if the given object is not an asynchronous iterable.

aiostream.aiter_utils.is_async_iterator(obj)

Check if the given object is an asynchronous iterator.

aiostream.aiter_utils.assert_async_iterator(obj)

Raise a TypeError if the given object is not an asynchronous iterator.

class aiostream.aiter_utils.AsyncIteratorContext(aiterator)

Asynchronous iterator with context management.

The context management makes sure the aclose asynchronous method of the corresponding iterator has run before it exits. It also issues warnings and RuntimeError if it is used incorrectly.

Correct usage:

ait = some_asynchronous_iterable()
async with AsyncIteratorContext(ait) as safe_ait:
    async for item in safe_ait:
        <block>

It is nonetheless not meant to use directly. Prefer aitercontext helper instead.

aiostream.aiter_utils.aitercontext(aiterable, *, cls=<class 'aiostream.aiter_utils.AsyncIteratorContext'>)

Return an asynchronous context manager from an asynchronous iterable.

The context management makes sure the aclose asynchronous method has run before it exits. It also issues warnings and RuntimeError if it is used incorrectly.

It is safe to use with any asynchronous iterable and prevent asynchronous iterator context to be wrapped twice.

Correct usage:

ait = some_asynchronous_iterable()
async with aitercontext(ait) as safe_ait:
    async for item in safe_ait:
        <block>

An optional subclass of AsyncIteratorContext can be provided. This class will be used to wrap the given iterable.