Generator-based resource management
A resource needs guaranteed cleanup without a dedicated context-manager class.
Python · Module
Python’s contextlib module provides helpers for building, composing, and adapting synchronous or asynchronous context managers. It simplifies deterministic setup and cleanup around with and async with blocks.
The contextlib module supplies reusable helpers for code that manages with-statement lifetimes. Its tools cover synchronous and asynchronous context managers, cleanup wrappers, exception handling, optional resources, and using context managers as decorators.
import contextlib
with contextlib.contextmanager(...):
...| Name | Type | Description |
|---|---|---|
various APIs | module members | The module exposes multiple classes, functions, and decorators; their parameters vary by API. |
The module's members return context managers, decorators, stacks, or wrapped values according to the selected API.
from contextlib import contextmanager
@contextmanager
def managed_resource():
resource = acquire_resource()
try:
yield resource
finally:
resource.close()
with managed_resource() as resource:
use(resource)The generator supplies the value at yield and performs cleanup afterward, including when the with block raises an exception.
A resource needs guaranteed cleanup without a dedicated context-manager class.
Resources are acquired conditionally or in a runtime-determined sequence.
A setup/teardown policy should apply to both a block and an entire function.
An optional dependency should expose one uniform with-statement interface.
contextmanager() turns a generator function into a context-manager factory. Code before yield runs on entry, the yielded object becomes the as value, and code after yield handles exit.
Follow-up: What happens if the with body raises? The exception is sent back into the generator at yield, so cleanup in finally still runs; suppressing it requires handling the exception appropriately.
ExitStack lets code enter several context managers dynamically and unwinds them in reverse order. It is useful when the number or kind of resources is determined at runtime.
Follow-up: How would you handle a failure while entering one resource? Register earlier resources through the stack and use its callbacks or context-manager entries so already-acquired resources are released.
ContextDecorator allows a context manager to work both in a with statement and as a function decorator. A decorated function invocation receives the same enter/exit-style management around its call.
Follow-up: Why can reuse matter? A decorator may invoke the manager repeatedly, so the manager must support the required reuse or reentrancy behavior.
AbstractAsyncContextManagerAbstractContextManagerAsyncExitStackContextDecoratorExitStackasynccontextmanagerclosingcontextmanager