Skip to content

Commit

Permalink
add AsyncExitStack examples
Browse files Browse the repository at this point in the history
  • Loading branch information
thehesiod committed May 14, 2020
1 parent b6fe319 commit 012a573
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,48 @@ Basic Example
loop.run_until_complete(go())
Context Manager Examples
------------------------

.. code:: python
from contextlib import AsyncExitStack
from typing import
import aiobotocore.session
# How to use in existing context manager
class Manager:
def __init__(self):
self._exit_stack = AsyncExitStack()
self._s3_client = None
async def __aenter__(self):
session = aiobotocore.session.AioSession()
self._s3_client = await self._exit_stack.enter_async_context(session.create_client('s3'))
async def __aexit__(self, exc_type, exc_val, exc_tb):
await self._exit_stack.__aexit__(exc_type, exc_val, exc_tb)
# How to use with an external exit_stack
async def create_s3_client(session: aiobotocore.session.AioSession, exit_stack: AsyncExitStack):
# Create client and add cleanup
client = await exit_stack.enter_async_context(session.create_client('s3'))
return client
async def non_manager_example():
session = aiobotocore.session.AioSession()
async with AsyncExitStack() as exit_stack:
s3_client = await create_s3_client(session, exit_stack)
# do work with s3_client
Supported AWS Services
----------------------

Expand Down

0 comments on commit 012a573

Please sign in to comment.