
Python Resource Management
This handles Python resource cleanup the right way with proper context managers, both sync and async variants. You get patterns for database connections, file handles, streaming responses that accumulate state, and the tricky bits like selective exception suppression and nested resource cleanup. The examples show real __enter__/__exit__ implementations, not just the contextlib shortcuts. Covers the cases where resources absolutely must be released even when things go wrong, which is most production code involving databases, files, or network connections.
- skills.sh installs
- 11.4K
- GitHub stars
- 42.4K
- last updated
- Aug 21, 2026
Install
$ npx -y skills add wshobson/agents --skill python-resource-management --agent claude-codeInstalls into .claude/skills of the current project.
Files
Python Resource Management
Manage resources deterministically using context managers. Resources like database connections, file handles, and network sockets should be released reliably, even when exceptions occur.
When to Use This Skill
- Managing database connections and connection pools
- Working with file handles and I/O
- Implementing custom context managers
- Building streaming responses with state
- Handling nested resource cleanup
- Creating async context managers
Core Concepts
1. Context Managers
The with statement ensures resources are released automatically, even on exceptions.
2. Protocol Methods
__enter__/__exit__ for sync, __aenter__/__aexit__ for async resource management.
3. Unconditional Cleanup
__exit__ always runs, regardless of whether an exception occurred.
4. Exception Handling
Return True from __exit__ to suppress exceptions, False to propagate them.
Quick Start
from contextlib import contextmanager
@contextmanager
def managed_resource():
resource = acquire_resource()
try:
yield resource
finally:
resource.cleanup()
Sponsors
Details
- Repository
- github.com/wshobson/agents
- Category
- Python
More from wshobson/agents
All 174- Binary Analysis Patterns11.4K
- Cqrs Implementation11.4K
- Workflow Patterns11.4K
- Incident Runbook Templates11.4K
- Airflow Dag Patterns11.3K
- Dbt Transformation Patterns11.3K
- Paypal Integration11.3K
- Temporal Python Testing11.3K
- Anti Reversing Techniques11.3K
- Angular Migration11.2K
- Nx Workspace Patterns11.2K
- Postmortem Writing11.2K
- Event Store Design11.2K
- Sast Configuration11.2K
- Hybrid Search Implementation11.2K
- Web3 Testing11.2K
- Distributed Tracing11.1K
- Screen Reader Testing11.1K
- Saga Orchestration11.1K
- Multi Cloud Architecture11.1K
- Spark Optimization11.1K
- Similarity Search Patterns11K
- Vector Index Tuning11K
- Hybrid Cloud Networking11K