python-resource-management

Implement Python context managers for deterministic resource cleanup and streaming state accumulation.

Updated Apr 23, 2026
One-click install
npx skills add https://github.com/SanketAdlak/PDMProjectDesign --skill python-resource-management-sanketadlak
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-resource-management
Source: https://github.com/SanketAdlak/PDMProjectDesign/tree/main/.agents/skills/python-resource-management
Command: npx skills add https://github.com/SanketAdlak/PDMProjectDesign --skill python-resource-management-sanketadlak

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Python resources like database connections, file handles, and network sockets leak when cleanup is forgotten or exceptions interrupt normal flow. This Skill provides patterns for guaranteeing resource release using context managers, plus techniques for streaming responses with accumulated state. ## Core Features & Use Cases - Context Manager Patterns: Class-based __enter__/__exit__ implementations, @contextmanager decorators, and async variants with __aenter__/__aexit__ for database pools and transactions. - Unconditional Cleanup: Guaranteed resource release even on exceptions, selective exception suppression, and multi-resource management with ExitStack/AsyncExitStack. - Streaming with State: Accumulate streamed chunks efficiently with list-join instead of O(n²) string concatenation, and track metrics like time-to-first-byte. - Use Case: When building an API endpoint that streams LLM responses while holding a database connection, use these patterns to ensure the connection closes even if the client disconnects mid-stream. ## Quick Start Show me how to write an async context manager that manages a database connection pool with guaranteed cleanup on exceptions.

Frequently Asked Questions about python-resource-management

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write a context manager in Python?

Implement `__enter__` and `__exit__` methods on a class, or use the `@contextmanager` decorator from contextlib with a generator that yields the resource. The `__exit__` method runs unconditionally, even when exceptions occur inside the `with` block.

How to manage database connections with async context managers?

Implement `__aenter__` and `__aexit__` to create and close async resources like asyncpg connection pools. Use `async with` to ensure the pool closes after operations complete, and acquire individual connections inside the pool context.

When should I use ExitStack instead of nested with statements?

Use ExitStack when managing a dynamic number of resources, such as opening an arbitrary list of files. It registers cleanup callbacks at runtime, avoiding deeply nested `with` blocks that static counts would require.

Why is string concatenation slow when accumulating stream chunks?

Python strings are immutable, so `content += chunk` creates a new string each iteration, producing O(n²) behavior. Append chunks to a list and call `"".join(chunks)` once for O(n) accumulation.

How do I suppress exceptions in a context manager safely?

Return `True` from `__exit__` only for specific, documented exception types like BrokenPipeError during client disconnects. Return `False` or None for all other exceptions so unexpected errors still propagate.