Skip to content

Commit

Permalink
Add closing_async() async context manager (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
achimnol committed Apr 11, 2022
1 parent 3848def commit b4d1a63
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions changes/48.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add the `closing_async()` async context manager, in addition to `aclosing()`
19 changes: 18 additions & 1 deletion src/aiotools/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
from typing import Any, Callable, Iterable, Optional, List

__all__ = [
'AsyncContextManager', 'async_ctx_manager', 'actxmgr', 'aclosing',
'AsyncContextManager', 'async_ctx_manager', 'actxmgr',
'aclosing', 'closing_async',
'AsyncContextGroup', 'actxgroup',
]

Expand Down Expand Up @@ -171,6 +172,22 @@ async def __aexit__(self, *args):
await self.thing.aclose()


class closing_async:
"""
An analogy to :func:`contextlib.closing` for objects with ``close()``
methods as async functions.
"""

def __init__(self, thing):
self.thing = thing

async def __aenter__(self):
return self.thing

async def __aexit__(self, *args):
await self.thing.close()


class AsyncContextGroup:
"""
Merges a group of context managers into a single context manager.
Expand Down

0 comments on commit b4d1a63

Please sign in to comment.