wshobson avatar

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-code

Installs 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

Recommended

More Python
1github avatarpython-mcp-server-generatorgithub/awesome-copilotGenerate a complete MCP server project in Python with tools, resources, and proper configuration10.5Kskills.sh installs
2github avatardataverse-python-production-codegithub/awesome-copilotGenerate production-ready Python code using Dataverse SDK with error handling, optimization, and best practices9.8Kskills.sh installs
3github avatardataverse-python-advanced-patternsgithub/awesome-copilotGenerate production code for Dataverse SDK using advanced patterns, error handling, and optimization techniques.9.2Kskills.sh installs
4github avatardataverse-python-quickstartgithub/awesome-copilotGenerate Python SDK setup + CRUD + bulk + paging snippets using official patterns.8.7Kskills.sh installs
5github avatardataverse-python-usecase-buildergithub/awesome-copilotGenerate complete solutions for specific Dataverse SDK use cases with architecture recommendations8.7Kskills.sh installs
6aws avataraws-sdk-python-usageaws/agent-toolkit-for-awsAWS SDK for Python (boto3/botocore) development patterns. You MUST use this skill when writing Python code that uses AWS services via boto3 or botocore. This in8.3Kskills.sh installs