Skip to main content

aclosing_iterator_wrapper

Wraps an asynchronous iterable to ensure a provided close callback is executed upon completion or failure, while temporarily disconnecting and reconnecting the Django request_finished signal to manage database connection cleanup.

def aclosing_iterator_wrapper(
iterable: AsyncIterable,
close: Callable
) - > AsyncIterator

Wraps an asynchronous iterable to ensure a cleanup callback is executed after iteration completes or an error occurs. It manages the Django request_finished signal connection to prevent premature connection closing during the cleanup process.

Parameters

NameTypeDescription
iterableAsyncIterableThe source asynchronous iterable to be consumed and yielded.
closeCallableA cleanup function invoked in the finally block to release resources or close connections.

Returns

TypeDescription
AsyncIteratorAn asynchronous generator that yields chunks from the original iterable.