aiorem

class aiorem.AbstractResourceManager

Bases: AbstractAsyncContextManager[AbstractResourceManager], AsyncContextDecorator, ABC

Abstract base class for asynchronous resource managers. Instances of this class can be used as asynchronous context managers while at the same time providing explicit methods to acquire and release resources.

The context manager usage

async with resource_manager as rm:
    # code

is roughly equivalent to

try:
    await rm.acquire_resources()
    # code
    await rm.release_resources()
except Exception as exc:
    await rm.release_resources_on_error()
    raise exc

See also

__aenter__() and __aexit__().

Instances of this class can also be used as decorators for asynchronous functions.

Example

@resource_manager
async def some_function():
    # code
async __aenter__() Self

Entry point for the asynchronous context manager.

Returns:

The initialized instance.

Raises:

Exception – If an exception is raised during acquiring resources, release_resources_on_error() is called.

async __aexit__(exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None) None

Exit point for the asynchronous context manager. Calls either release_resources() or release_resources_on_error() depending on whether an exception occurred in the context block.

Returns:

None

abstract async acquire_resources() None

Acquire resources used by this class. Must be implemented by a subclass.

Returns:

None

abstract async release_resources() None

Release resources used by this class. Must be implemented by a subclass.

Returns:

None

async release_resources_on_error() None

Release resources used by this class in case of an error either in acquire_resources() or in the context block of the context manager. Defaults to calling release_resources().

class aiorem.AbstractResourceManagerCollection

Bases: AbstractResourceManager, ABC

Abstract base class for collections of asynchronous resource managers. This is a simple implementation of AbstractResourceManager that manages a collection of AbstractResourceManager instances.

abstract property _resource_managers: Collection[AbstractResourceManager]

Return a collection of AbstractResourceManager instances used by this resource manager collection. Must be implemented by a subclass.

Return type:

Collection[AbstractResourceManager]

async acquire_resources() None

Calls acquire_resources() on all resource managers in the _resource_managers in a concurrent fashion. If an exception occurs during the acquisition of resources, the already acquired resources are released.

Returns:

None

Raises:

ExceptionGroup – If an error occurs while acquiring resources, an ExceptionGroup is raised containing all exceptions that occurred.

async release_resources() None

Calls release_resources() on all resource managers in the _resource_managers in a concurrent fashion. If an exception occurs during the release of resources, the remaining resources are still released.

Returns:

None

Raises:

ExceptionGroup – If an error occurs while releasing resources, an ExceptionGroup is raised containing all exceptions that occurred.

async release_resources_on_error() None

Like release_resources(), but calls release_resources_on_error() on all resource managers in the _resource_managers.

Returns:

None

Raises:

ExceptionGroup – If an error occurs while releasing resources, an ExceptionGroup is raised containing all exceptions that occurred.