Lines Matching +full:async +full:- +full:io
1 :mod:`!contextlib` --- Utilities for :keyword:`!with`\ -statement contexts
5 :synopsis: Utilities for with-statement contexts.
9 --------------
13 :ref:`context-managers`.
17 ---------
39 :ref:`async-context-managers`.
75 The function being decorated must return a :term:`generator`-iterator when
94 each function call (this allows the otherwise "one-shot" context managers
105 :ref:`asynchronous context manager <async-context-managers>`.
108 function for :keyword:`async with` statement asynchronous context managers,
118 async def get_connection():
125 async def get_all_users():
126 async with get_connection() as conn:
132 either as decorators or with :keyword:`async with` statements::
138 async def timeit():
143 print(f'it took {time.monotonic() - now}s to run')
146 async def main():
147 # ... async code ...
150 each function call. This allows the otherwise "one-shot" context managers
155 Async context managers created with :func:`asynccontextmanager` can
188 Return an async context manager that calls the ``aclose()`` method of *thing*
194 async def aclosing(thing):
200 Significantly, ``aclosing()`` supports deterministic cleanup of async
206 async with aclosing(my_generator()) as values:
207 async for value in values:
211 This pattern ensures that the generator's async exit code is executed in
219 .. _simplifying-support-for-single-optional-context-managers:
224 otherwise does nothing. It is intended to be used as a stand-in for an
250 It can also be used as a stand-in for
251 :ref:`asynchronous context managers <async-context-managers>`::
253 async def send_http(session=None):
261 async with cm as session:
305 This context manager is :ref:`reentrant <reentrant-cms>`.
313 another file or file-like object.
320 :class:`io.StringIO` object. The replacement stream is returned from the
324 with redirect_stdout(io.StringIO()) as f:
345 This context manager is :ref:`reentrant <reentrant-cms>`.
353 :data:`sys.stderr` to another file or file-like object.
355 This context manager is :ref:`reentrant <reentrant-cms>`.
362 Non parallel-safe context manager to change the current working directory.
364 for use in most threaded or async contexts. It is also not suitable for most
365 non-linear code execution, like generators, where the program execution is
366 temporarily relinquished -- unless explicitly desired, you should not yield
372 This context manager is :ref:`reentrant <reentrant-cms>`.
465 async def __aenter__(self):
469 async def __aexit__(self, *exc):
476 ... async def function():
484 >>> async def function():
485 ... async with mycontext():
526 extends to exception handling - if an inner callback suppresses or replaces
582 and returns it. No callbacks are invoked by this operation - instead,
607 An :ref:`asynchronous context manager <async-context-managers>`, similar
639 async with AsyncExitStack() as stack:
643 # the async with statement, even if attempts to open a connection
649 --------------------
751 Replacing any use of ``try-finally`` and flag variables
754 A pattern you will sometimes see is a ``try-finally`` statement with a flag
872 :pep:`343` - The "with" statement
876 .. _single-use-reusable-and-reentrant-cms:
879 ---------------------------------------------------
883 context managers must be created afresh each time they're used -
893 further IO operations using that file object.
920 .. _reentrant-cms:
935 >>> from io import StringIO
959 .. _reusable-cms: