• Home
  • Raw
  • Download

Lines Matching +full:resource +full:- +full:manager

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`.
51 resource needs to be managed that isn't a context manager in its own right,
54 An abstract example would be the following to ensure correct resource
61 # Code to acquire resource, e.g.:
62 resource = acquire_resource(*args, **kwds)
64 yield resource
66 # Code to release resource, e.g.:
67 release_resource(resource)
71 >>> with managed_resource(timeout=3600) as resource:
72 ... # Resource is released at the end of this block,
75 The function being decorated must return a :term:`generator`-iterator when
87 generator context manager will indicate to the :keyword:`!with` statement that
94 each function call (this allows the otherwise "one-shot" context managers
105 :ref:`asynchronous context manager <async-context-managers>`.
143 print(f'it took {time.monotonic() - now}s to run')
150 each function call. This allows the otherwise "one-shot" context managers
161 Return a context manager that closes *thing* upon completion of the block. This
188 Return an async context manager that calls the ``aclose()`` method of *thing*
219 .. _simplifying-support-for-single-optional-context-managers:
223 Return a context manager that returns *enter_result* from ``__enter__``, but
224 otherwise does nothing. It is intended to be used as a stand-in for an
225 optional context manager, for example::
250 It can also be used as a stand-in for
251 :ref:`asynchronous context managers <async-context-managers>`::
267 :term:`asynchronous context manager` support was added.
273 Return a context manager that suppresses any of the specified exceptions
279 context manager should be used only to cover very specific errors where
305 This context manager is :ref:`reentrant <reentrant-cms>`.
312 Context manager for temporarily redirecting :data:`sys.stdout` to
313 another file or file-like object.
341 context manager is not suitable for use in library code and most threaded
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.
365 non-linear code execution, like generators, where the program execution is
366 temporarily relinquished -- unless explicitly desired, you should not yield
367 when this context manager is active.
372 This context manager is :ref:`reentrant <reentrant-cms>`.
379 A base class that enables a context manager to also be used as a decorator.
448 underlying context manager must support use in multiple :keyword:`with`
498 A context manager that is designed to make it easy to programmatically
526 extends to exception handling - if an inner callback suppresses or replaces
539 Enters a new context manager and adds its :meth:`__exit__` method to
541 manager's own :meth:`__enter__` method.
548 is not a context manager.
552 Adds a context manager's :meth:`__exit__` method to the callback stack.
555 part of an :meth:`__enter__` implementation with a context manager's own
558 If passed an object that is not a context manager, this method assumes
559 it is a callback with the same signature as a context manager's
563 same way context manager :meth:`__exit__` methods can.
582 and returns it. No callbacks are invoked by this operation - instead,
607 An :ref:`asynchronous context manager <async-context-managers>`, similar
618 manager.
622 is not an asynchronous context manager.
626 Similar to :meth:`push` but expects either an asynchronous context manager
649 --------------------
666 for resource in resources:
667 stack.enter_context(resource)
683 the :keyword:`with` statement body or the context manager's ``__exit__``
697 should be providing a direct resource management interface for use with
699 all APIs are well designed in that regard. When a context manager is the
700 only resource management API provided, then :class:`ExitStack` can make it
709 method can be useful in cleaning up an already allocated resource if later
712 Here's an example of doing this for a context manager that accepts resource
724 def check_resource_ok(resource):
734 # Accordingly, we want to keep the resource, and pass it
739 resource = self.acquire_resource()
741 if not self.check_resource_ok(resource):
743 raise RuntimeError(msg.format(resource))
744 return resource
747 # We don't need to duplicate any of our resource release logic
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
805 If the resource cleanup isn't already neatly bundled into a standalone
807 :meth:`ExitStack.callback` to declare the resource cleanup in
825 Using a context manager as a function decorator
828 :class:`ContextDecorator` makes it possible to use a context manager in
833 writing both a function decorator and a context manager for the task,
852 Instances of this class can be used as both a context manager::
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 -
920 .. _reentrant-cms:
928 using the same context manager.
930 :class:`threading.RLock` is an example of a reentrant context manager, as are
959 .. _reusable-cms:
968 will fail (or otherwise not work correctly) if the specific context manager
972 context manager (for a reentrant lock, it is necessary to use
975 Another example of a reusable, but not reentrant, context manager is