[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M7BWMRVK6VMBSAP8ZETFN2BY":3,"skill-same:01M7BWMRVK6VMBSAP8ZETFN2BY":47},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":16,"categories":17,"keywords":18,"publishedAt":19,"linkOnly":20,"markdownContent":21,"markdownBody":22,"frontMatter":23,"refName":24,"commitSha":25,"skillDirectory":26,"forksCount":27,"repositoryHtmlUrl":28,"repositoryHomepageUrl":29,"ownerHtmlUrl":30,"ownerBlogUrl":6,"ownerDescription":31,"updatedAt":32,"install":33,"resources":46},"01M7BWMRVK6VMBSAP8ZETFN2BY","python-resource-management",null,"Python resource management with context managers, cleanup patterns, and streaming. Use when managing connections, file handles, implementing cleanup logic, or building streaming responses with accumulated state.","YYC-Cube\u002FYYC3-Knowledge-Base","YYC-Cube","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F235006543?v=4","01M79J2PH4SWD2X0825X6PN748","yyc3","01M79J2PHVK3F6ZBYT8K2SCZCW",1,"MIT","https:\u002F\u002Fgithub.com\u002FYYC-Cube\u002FYYC3-Knowledge-Base\u002Fblob\u002Fmain\u002FYYC3-Plugin-插件协议\u002FYYC3-Plugin-插件协议-plugins-claude\u002Fpython-development\u002Fskills\u002Fpython-resource-management\u002FSKILL.md",[],[],"2026-11-15 14:19:17",false,"---\nname: python-resource-management\ndescription: Python resource management with context managers, cleanup patterns, and streaming. Use when managing connections, file handles, implementing cleanup logic, or building streaming responses with accumulated state.\n---\n\n# Python Resource Management\n\nManage resources deterministically using context managers. Resources like database connections, file handles, and network sockets should be released reliably, even when exceptions occur.\n\n## When to Use This Skill\n\n- Managing database connections and connection pools\n- Working with file handles and I\u002FO\n- Implementing custom context managers\n- Building streaming responses with state\n- Handling nested resource cleanup\n- Creating async context managers\n\n## Core Concepts\n\n### 1. Context Managers\n\nThe `with` statement ensures resources are released automatically, even on exceptions.\n\n### 2. Protocol Methods\n\n`__enter__`\u002F`__exit__` for sync, `__aenter__`\u002F`__aexit__` for async resource management.\n\n### 3. Unconditional Cleanup\n\n`__exit__` always runs, regardless of whether an exception occurred.\n\n### 4. Exception Handling\n\nReturn `True` from `__exit__` to suppress exceptions, `False` to propagate them.\n\n## Quick Start\n\n```python\nfrom contextlib import contextmanager\n\n@contextmanager\ndef managed_resource():\n    resource = acquire_resource()\n    try:\n        yield resource\n    finally:\n        resource.cleanup()\n\nwith managed_resource() as r:\n    r.do_work()\n```\n\n## Fundamental Patterns\n\n### Pattern 1: Class-Based Context Manager\n\nImplement the context manager protocol for complex resources.\n\n```python\nclass DatabaseConnection:\n    \"\"\"Database connection with automatic cleanup.\"\"\"\n\n    def __init__(self, dsn: str) -> None:\n        self._dsn = dsn\n        self._conn: Connection | None = None\n\n    def connect(self) -> None:\n        \"\"\"Establish database connection.\"\"\"\n        self._conn = psycopg.connect(self._dsn)\n\n    def close(self) -> None:\n        \"\"\"Close connection if open.\"\"\"\n        if self._conn is not None:\n            self._conn.close()\n            self._conn = None\n\n    def __enter__(self) -> \"DatabaseConnection\":\n        \"\"\"Enter context: connect and return self.\"\"\"\n        self.connect()\n        return self\n\n    def __exit__(\n        self,\n        exc_type: type[BaseException] | None,\n        exc_val: BaseException | None,\n        exc_tb: TracebackType | None,\n    ) -> None:\n        \"\"\"Exit context: always close connection.\"\"\"\n        self.close()\n\n# Usage with context manager (preferred)\nwith DatabaseConnection(dsn) as db:\n    result = db.execute(query)\n\n# Manual management when needed\ndb = DatabaseConnection(dsn)\ndb.connect()\ntry:\n    result = db.execute(query)\nfinally:\n    db.close()\n```\n\n### Pattern 2: Async Context Manager\n\nFor async resources, implement the async protocol.\n\n```python\nclass AsyncDatabasePool:\n    \"\"\"Async database connection pool.\"\"\"\n\n    def __init__(self, dsn: str, min_size: int = 1, max_size: int = 10) -> None:\n        self._dsn = dsn\n        self._min_size = min_size\n        self._max_size = max_size\n        self._pool: asyncpg.Pool | None = None\n\n    async def __aenter__(self) -> \"AsyncDatabasePool\":\n        \"\"\"Create connection pool.\"\"\"\n        self._pool = await asyncpg.create_pool(\n            self._dsn,\n            min_size=self._min_size,\n            max_size=self._max_size,\n        )\n        return self\n\n    async def __aexit__(\n        self,\n        exc_type: type[BaseException] | None,\n        exc_val: BaseException | None,\n        exc_tb: TracebackType | None,\n    ) -> None:\n        \"\"\"Close all connections in pool.\"\"\"\n        if self._pool is not None:\n            await self._pool.close()\n\n    async def execute(self, query: str, *args) -> list[dict]:\n        \"\"\"Execute query using pooled connection.\"\"\"\n        async with self._pool.acquire() as conn:\n            return await conn.fetch(query, *args)\n\n# Usage\nasync with AsyncDatabasePool(dsn) as pool:\n    users = await pool.execute(\"SELECT * FROM users WHERE active = $1\", True)\n```\n\n### Pattern 3: Using @contextmanager Decorator\n\nSimplify context managers with the decorator for straightforward cases.\n\n```python\nfrom contextlib import contextmanager, asynccontextmanager\nimport time\nimport structlog\n\nlogger = structlog.get_logger()\n\n@contextmanager\ndef timed_block(name: str):\n    \"\"\"Time a block of code.\"\"\"\n    start = time.perf_counter()\n    try:\n        yield\n    finally:\n        elapsed = time.perf_counter() - start\n        logger.info(f\"{name} completed\", duration_seconds=round(elapsed, 3))\n\n# Usage\nwith timed_block(\"data_processing\"):\n    process_large_dataset()\n\n@asynccontextmanager\nasync def database_transaction(conn: AsyncConnection):\n    \"\"\"Manage database transaction.\"\"\"\n    await conn.execute(\"BEGIN\")\n    try:\n        yield conn\n        await conn.execute(\"COMMIT\")\n    except Exception:\n        await conn.execute(\"ROLLBACK\")\n        raise\n\n# Usage\nasync with database_transaction(conn) as tx:\n    await tx.execute(\"INSERT INTO users ...\")\n    await tx.execute(\"INSERT INTO audit_log ...\")\n```\n\n### Pattern 4: Unconditional Resource Release\n\nAlways clean up resources in `__exit__`, regardless of exceptions.\n\n```python\nclass FileProcessor:\n    \"\"\"Process file with guaranteed cleanup.\"\"\"\n\n    def __init__(self, path: str) -> None:\n        self._path = path\n        self._file: IO | None = None\n        self._temp_files: list[Path] = []\n\n    def __enter__(self) -> \"FileProcessor\":\n        self._file = open(self._path, \"r\")\n        return self\n\n    def __exit__(\n        self,\n        exc_type: type[BaseException] | None,\n        exc_val: BaseException | None,\n        exc_tb: TracebackType | None,\n    ) -> None:\n        \"\"\"Clean up all resources unconditionally.\"\"\"\n        # Close main file\n        if self._file is not None:\n            self._file.close()\n\n        # Clean up any temporary files\n        for temp_file in self._temp_files:\n            try:\n                temp_file.unlink()\n            except OSError:\n                pass  # Best effort cleanup\n\n        # Return None\u002FFalse to propagate any exception\n```\n\n## Advanced Patterns\n\n### Pattern 5: Selective Exception Suppression\n\nOnly suppress specific, documented exceptions.\n\n```python\nclass StreamWriter:\n    \"\"\"Writer that handles broken pipe gracefully.\"\"\"\n\n    def __init__(self, stream) -> None:\n        self._stream = stream\n\n    def __enter__(self) -> \"StreamWriter\":\n        return self\n\n    def __exit__(\n        self,\n        exc_type: type[BaseException] | None,\n        exc_val: BaseException | None,\n        exc_tb: TracebackType | None,\n    ) -> bool:\n        \"\"\"Clean up, suppressing BrokenPipeError on shutdown.\"\"\"\n        self._stream.close()\n\n        # Suppress BrokenPipeError (client disconnected)\n        # This is expected behavior, not an error\n        if exc_type is BrokenPipeError:\n            return True  # Exception suppressed\n\n        return False  # Propagate all other exceptions\n```\n\n### Pattern 6: Streaming with Accumulated State\n\nMaintain both incremental chunks and accumulated state during streaming.\n\n```python\nfrom collections.abc import Generator\nfrom dataclasses import dataclass, field\n\n@dataclass\nclass StreamingResult:\n    \"\"\"Accumulated streaming result.\"\"\"\n\n    chunks: list[str] = field(default_factory=list)\n    _finalized: bool = False\n\n    @property\n    def content(self) -> str:\n        \"\"\"Get accumulated content.\"\"\"\n        return \"\".join(self.chunks)\n\n    def add_chunk(self, chunk: str) -> None:\n        \"\"\"Add chunk to accumulator.\"\"\"\n        if self._finalized:\n            raise RuntimeError(\"Cannot add to finalized result\")\n        self.chunks.append(chunk)\n\n    def finalize(self) -> str:\n        \"\"\"Mark stream complete and return content.\"\"\"\n        self._finalized = True\n        return self.content\n\ndef stream_with_accumulation(\n    response: StreamingResponse,\n) -> Generator[tuple[str, str], None, str]:\n    \"\"\"Stream response while accumulating content.\n\n    Yields:\n        Tuple of (accumulated_content, new_chunk) for each chunk.\n\n    Returns:\n        Final accumulated content.\n    \"\"\"\n    result = StreamingResult()\n\n    for chunk in response.iter_content():\n        result.add_chunk(chunk)\n        yield result.content, chunk\n\n    return result.finalize()\n```\n\n### Pattern 7: Efficient String Accumulation\n\nAvoid O(n²) string concatenation when accumulating.\n\n```python\ndef accumulate_stream(stream) -> str:\n    \"\"\"Efficiently accumulate stream content.\"\"\"\n    # BAD: O(n²) due to string immutability\n    # content = \"\"\n    # for chunk in stream:\n    #     content += chunk  # Creates new string each time\n\n    # GOOD: O(n) with list and join\n    chunks: list[str] = []\n    for chunk in stream:\n        chunks.append(chunk)\n    return \"\".join(chunks)  # Single allocation\n```\n\n### Pattern 8: Tracking Stream Metrics\n\nMeasure time-to-first-byte and total streaming time.\n\n```python\nimport time\nfrom collections.abc import Generator\n\ndef stream_with_metrics(\n    response: StreamingResponse,\n) -> Generator[str, None, dict]:\n    \"\"\"Stream response while collecting metrics.\n\n    Yields:\n        Content chunks.\n\n    Returns:\n        Metrics dictionary.\n    \"\"\"\n    start = time.perf_counter()\n    first_chunk_time: float | None = None\n    chunk_count = 0\n    total_bytes = 0\n\n    for chunk in response.iter_content():\n        if first_chunk_time is None:\n            first_chunk_time = time.perf_counter() - start\n\n        chunk_count += 1\n        total_bytes += len(chunk.encode())\n        yield chunk\n\n    total_time = time.perf_counter() - start\n\n    return {\n        \"time_to_first_byte_ms\": round((first_chunk_time or 0) * 1000, 2),\n        \"total_time_ms\": round(total_time * 1000, 2),\n        \"chunk_count\": chunk_count,\n        \"total_bytes\": total_bytes,\n    }\n```\n\n### Pattern 9: Managing Multiple Resources with ExitStack\n\nHandle a dynamic number of resources cleanly.\n\n```python\nfrom contextlib import ExitStack, AsyncExitStack\nfrom pathlib import Path\n\ndef process_files(paths: list[Path]) -> list[str]:\n    \"\"\"Process multiple files with automatic cleanup.\"\"\"\n    results = []\n\n    with ExitStack() as stack:\n        # Open all files - they'll all be closed when block exits\n        files = [stack.enter_context(open(p)) for p in paths]\n\n        for f in files:\n            results.append(f.read())\n\n    return results\n\nasync def process_connections(hosts: list[str]) -> list[dict]:\n    \"\"\"Process multiple async connections.\"\"\"\n    results = []\n\n    async with AsyncExitStack() as stack:\n        connections = [\n            await stack.enter_async_context(connect_to_host(host))\n            for host in hosts\n        ]\n\n        for conn in connections:\n            results.append(await conn.fetch_data())\n\n    return results\n```\n\n## Best Practices Summary\n\n1. **Always use context managers** - For any resource that needs cleanup\n2. **Clean up unconditionally** - `__exit__` runs even on exception\n3. **Don't suppress unexpectedly** - Return `False` unless suppression is intentional\n4. **Use @contextmanager** - For simple resource patterns\n5. **Implement both protocols** - Support `with` and manual management\n6. **Use ExitStack** - For dynamic numbers of resources\n7. **Accumulate efficiently** - List + join, not string concatenation\n8. **Track metrics** - Time-to-first-byte matters for streaming\n9. **Document behavior** - Especially exception suppression\n10. **Test cleanup paths** - Verify resources are released on errors\n","# Python Resource Management\n\nManage resources deterministically using context managers. Resources like database connections, file handles, and network sockets should be released reliably, even when exceptions occur.\n\n## When to Use This Skill\n\n- Managing database connections and connection pools\n- Working with file handles and I\u002FO\n- Implementing custom context managers\n- Building streaming responses with state\n- Handling nested resource cleanup\n- Creating async context managers\n\n## Core Concepts\n\n### 1. Context Managers\n\nThe `with` statement ensures resources are released automatically, even on exceptions.\n\n### 2. Protocol Methods\n\n`__enter__`\u002F`__exit__` for sync, `__aenter__`\u002F`__aexit__` for async resource management.\n\n### 3. Unconditional Cleanup\n\n`__exit__` always runs, regardless of whether an exception occurred.\n\n### 4. Exception Handling\n\nReturn `True` from `__exit__` to suppress exceptions, `False` to propagate them.\n\n## Quick Start\n\n```python\nfrom contextlib import contextmanager\n\n@contextmanager\ndef managed_resource():\n    resource = acquire_resource()\n    try:\n        yield resource\n    finally:\n        resource.cleanup()\n\nwith managed_resource() as r:\n    r.do_work()\n```\n\n## Fundamental Patterns\n\n### Pattern 1: Class-Based Context Manager\n\nImplement the context manager protocol for complex resources.\n\n```python\nclass DatabaseConnection:\n    \"\"\"Database connection with automatic cleanup.\"\"\"\n\n    def __init__(self, dsn: str) -> None:\n        self._dsn = dsn\n        self._conn: Connection | None = None\n\n    def connect(self) -> None:\n        \"\"\"Establish database connection.\"\"\"\n        self._conn = psycopg.connect(self._dsn)\n\n    def close(self) -> None:\n        \"\"\"Close connection if open.\"\"\"\n        if self._conn is not None:\n            self._conn.close()\n            self._conn = None\n\n    def __enter__(self) -> \"DatabaseConnection\":\n        \"\"\"Enter context: connect and return self.\"\"\"\n        self.connect()\n        return self\n\n    def __exit__(\n        self,\n        exc_type: type[BaseException] | None,\n        exc_val: BaseException | None,\n        exc_tb: TracebackType | None,\n    ) -> None:\n        \"\"\"Exit context: always close connection.\"\"\"\n        self.close()\n\n# Usage with context manager (preferred)\nwith DatabaseConnection(dsn) as db:\n    result = db.execute(query)\n\n# Manual management when needed\ndb = DatabaseConnection(dsn)\ndb.connect()\ntry:\n    result = db.execute(query)\nfinally:\n    db.close()\n```\n\n### Pattern 2: Async Context Manager\n\nFor async resources, implement the async protocol.\n\n```python\nclass AsyncDatabasePool:\n    \"\"\"Async database connection pool.\"\"\"\n\n    def __init__(self, dsn: str, min_size: int = 1, max_size: int = 10) -> None:\n        self._dsn = dsn\n        self._min_size = min_size\n        self._max_size = max_size\n        self._pool: asyncpg.Pool | None = None\n\n    async def __aenter__(self) -> \"AsyncDatabasePool\":\n        \"\"\"Create connection pool.\"\"\"\n        self._pool = await asyncpg.create_pool(\n            self._dsn,\n            min_size=self._min_size,\n            max_size=self._max_size,\n        )\n        return self\n\n    async def __aexit__(\n        self,\n        exc_type: type[BaseException] | None,\n        exc_val: BaseException | None,\n        exc_tb: TracebackType | None,\n    ) -> None:\n        \"\"\"Close all connections in pool.\"\"\"\n        if self._pool is not None:\n            await self._pool.close()\n\n    async def execute(self, query: str, *args) -> list[dict]:\n        \"\"\"Execute query using pooled connection.\"\"\"\n        async with self._pool.acquire() as conn:\n            return await conn.fetch(query, *args)\n\n# Usage\nasync with AsyncDatabasePool(dsn) as pool:\n    users = await pool.execute(\"SELECT * FROM users WHERE active = $1\", True)\n```\n\n### Pattern 3: Using @contextmanager Decorator\n\nSimplify context managers with the decorator for straightforward cases.\n\n```python\nfrom contextlib import contextmanager, asynccontextmanager\nimport time\nimport structlog\n\nlogger = structlog.get_logger()\n\n@contextmanager\ndef timed_block(name: str):\n    \"\"\"Time a block of code.\"\"\"\n    start = time.perf_counter()\n    try:\n        yield\n    finally:\n        elapsed = time.perf_counter() - start\n        logger.info(f\"{name} completed\", duration_seconds=round(elapsed, 3))\n\n# Usage\nwith timed_block(\"data_processing\"):\n    process_large_dataset()\n\n@asynccontextmanager\nasync def database_transaction(conn: AsyncConnection):\n    \"\"\"Manage database transaction.\"\"\"\n    await conn.execute(\"BEGIN\")\n    try:\n        yield conn\n        await conn.execute(\"COMMIT\")\n    except Exception:\n        await conn.execute(\"ROLLBACK\")\n        raise\n\n# Usage\nasync with database_transaction(conn) as tx:\n    await tx.execute(\"INSERT INTO users ...\")\n    await tx.execute(\"INSERT INTO audit_log ...\")\n```\n\n### Pattern 4: Unconditional Resource Release\n\nAlways clean up resources in `__exit__`, regardless of exceptions.\n\n```python\nclass FileProcessor:\n    \"\"\"Process file with guaranteed cleanup.\"\"\"\n\n    def __init__(self, path: str) -> None:\n        self._path = path\n        self._file: IO | None = None\n        self._temp_files: list[Path] = []\n\n    def __enter__(self) -> \"FileProcessor\":\n        self._file = open(self._path, \"r\")\n        return self\n\n    def __exit__(\n        self,\n        exc_type: type[BaseException] | None,\n        exc_val: BaseException | None,\n        exc_tb: TracebackType | None,\n    ) -> None:\n        \"\"\"Clean up all resources unconditionally.\"\"\"\n        # Close main file\n        if self._file is not None:\n            self._file.close()\n\n        # Clean up any temporary files\n        for temp_file in self._temp_files:\n            try:\n                temp_file.unlink()\n            except OSError:\n                pass  # Best effort cleanup\n\n        # Return None\u002FFalse to propagate any exception\n```\n\n## Advanced Patterns\n\n### Pattern 5: Selective Exception Suppression\n\nOnly suppress specific, documented exceptions.\n\n```python\nclass StreamWriter:\n    \"\"\"Writer that handles broken pipe gracefully.\"\"\"\n\n    def __init__(self, stream) -> None:\n        self._stream = stream\n\n    def __enter__(self) -> \"StreamWriter\":\n        return self\n\n    def __exit__(\n        self,\n        exc_type: type[BaseException] | None,\n        exc_val: BaseException | None,\n        exc_tb: TracebackType | None,\n    ) -> bool:\n        \"\"\"Clean up, suppressing BrokenPipeError on shutdown.\"\"\"\n        self._stream.close()\n\n        # Suppress BrokenPipeError (client disconnected)\n        # This is expected behavior, not an error\n        if exc_type is BrokenPipeError:\n            return True  # Exception suppressed\n\n        return False  # Propagate all other exceptions\n```\n\n### Pattern 6: Streaming with Accumulated State\n\nMaintain both incremental chunks and accumulated state during streaming.\n\n```python\nfrom collections.abc import Generator\nfrom dataclasses import dataclass, field\n\n@dataclass\nclass StreamingResult:\n    \"\"\"Accumulated streaming result.\"\"\"\n\n    chunks: list[str] = field(default_factory=list)\n    _finalized: bool = False\n\n    @property\n    def content(self) -> str:\n        \"\"\"Get accumulated content.\"\"\"\n        return \"\".join(self.chunks)\n\n    def add_chunk(self, chunk: str) -> None:\n        \"\"\"Add chunk to accumulator.\"\"\"\n        if self._finalized:\n            raise RuntimeError(\"Cannot add to finalized result\")\n        self.chunks.append(chunk)\n\n    def finalize(self) -> str:\n        \"\"\"Mark stream complete and return content.\"\"\"\n        self._finalized = True\n        return self.content\n\ndef stream_with_accumulation(\n    response: StreamingResponse,\n) -> Generator[tuple[str, str], None, str]:\n    \"\"\"Stream response while accumulating content.\n\n    Yields:\n        Tuple of (accumulated_content, new_chunk) for each chunk.\n\n    Returns:\n        Final accumulated content.\n    \"\"\"\n    result = StreamingResult()\n\n    for chunk in response.iter_content():\n        result.add_chunk(chunk)\n        yield result.content, chunk\n\n    return result.finalize()\n```\n\n### Pattern 7: Efficient String Accumulation\n\nAvoid O(n²) string concatenation when accumulating.\n\n```python\ndef accumulate_stream(stream) -> str:\n    \"\"\"Efficiently accumulate stream content.\"\"\"\n    # BAD: O(n²) due to string immutability\n    # content = \"\"\n    # for chunk in stream:\n    #     content += chunk  # Creates new string each time\n\n    # GOOD: O(n) with list and join\n    chunks: list[str] = []\n    for chunk in stream:\n        chunks.append(chunk)\n    return \"\".join(chunks)  # Single allocation\n```\n\n### Pattern 8: Tracking Stream Metrics\n\nMeasure time-to-first-byte and total streaming time.\n\n```python\nimport time\nfrom collections.abc import Generator\n\ndef stream_with_metrics(\n    response: StreamingResponse,\n) -> Generator[str, None, dict]:\n    \"\"\"Stream response while collecting metrics.\n\n    Yields:\n        Content chunks.\n\n    Returns:\n        Metrics dictionary.\n    \"\"\"\n    start = time.perf_counter()\n    first_chunk_time: float | None = None\n    chunk_count = 0\n    total_bytes = 0\n\n    for chunk in response.iter_content():\n        if first_chunk_time is None:\n            first_chunk_time = time.perf_counter() - start\n\n        chunk_count += 1\n        total_bytes += len(chunk.encode())\n        yield chunk\n\n    total_time = time.perf_counter() - start\n\n    return {\n        \"time_to_first_byte_ms\": round((first_chunk_time or 0) * 1000, 2),\n        \"total_time_ms\": round(total_time * 1000, 2),\n        \"chunk_count\": chunk_count,\n        \"total_bytes\": total_bytes,\n    }\n```\n\n### Pattern 9: Managing Multiple Resources with ExitStack\n\nHandle a dynamic number of resources cleanly.\n\n```python\nfrom contextlib import ExitStack, AsyncExitStack\nfrom pathlib import Path\n\ndef process_files(paths: list[Path]) -> list[str]:\n    \"\"\"Process multiple files with automatic cleanup.\"\"\"\n    results = []\n\n    with ExitStack() as stack:\n        # Open all files - they'll all be closed when block exits\n        files = [stack.enter_context(open(p)) for p in paths]\n\n        for f in files:\n            results.append(f.read())\n\n    return results\n\nasync def process_connections(hosts: list[str]) -> list[dict]:\n    \"\"\"Process multiple async connections.\"\"\"\n    results = []\n\n    async with AsyncExitStack() as stack:\n        connections = [\n            await stack.enter_async_context(connect_to_host(host))\n            for host in hosts\n        ]\n\n        for conn in connections:\n            results.append(await conn.fetch_data())\n\n    return results\n```\n\n## Best Practices Summary\n\n1. **Always use context managers** - For any resource that needs cleanup\n2. **Clean up unconditionally** - `__exit__` runs even on exception\n3. **Don't suppress unexpectedly** - Return `False` unless suppression is intentional\n4. **Use @contextmanager** - For simple resource patterns\n5. **Implement both protocols** - Support `with` and manual management\n6. **Use ExitStack** - For dynamic numbers of resources\n7. **Accumulate efficiently** - List + join, not string concatenation\n8. **Track metrics** - Time-to-first-byte matters for streaming\n9. **Document behavior** - Especially exception suppression\n10. **Test cleanup paths** - Verify resources are released on errors\n",{"name":5,"description":7},"main","87afe6ce96220accf50a345dea46784c8c9f210e","YYC3-Plugin-插件协议\u002FYYC3-Plugin-插件协议-plugins-claude\u002Fpython-development\u002Fskills\u002Fpython-resource-management",0,"https:\u002F\u002Fgithub.com\u002FYYC-Cube\u002FYYC3-Knowledge-Base","https:\u002F\u002Fkb.yyc3.top\u002F","https:\u002F\u002Fgithub.com\u002FYYC-Cube","万象归元于云枢 | 深栈智启新纪元","2026-11-10 10:37:32",{"source":8,"skillName":5,"commands":34,"prompt":44,"zipAvailable":45},[35,38,41],{"tool":36,"command":37},"npx","npx skills add YYC-Cube\u002FYYC3-Knowledge-Base --skill python-resource-management",{"tool":39,"command":40},"bunx","bunx skills add YYC-Cube\u002FYYC3-Knowledge-Base --skill python-resource-management",{"tool":42,"command":43},"pnpm","pnpm dlx skills add YYC-Cube\u002FYYC3-Knowledge-Base --skill python-resource-management","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002FYYC-Cube\u002FYYC3-Knowledge-Base\u002Fblob\u002Fmain\u002FYYC3-Plugin-插件协议\u002FYYC3-Plugin-插件协议-plugins-claude\u002Fpython-development\u002Fskills\u002Fpython-resource-management\u002FSKILL.md",true,[],[48,55,62,69,76,83],{"skillNo":49,"name":50,"nameZh":6,"description":51,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":52,"categories":53,"keywords":54,"publishedAt":19},"01M7CTDWHERCXPQCP0K03A86AS","vercel-sandbox","Run agent-browser + Chrome inside Vercel Sandbox microVMs for browser automation from any Vercel-deployed app. Use when the user needs browser automation in a Vercel app (Next.js, SvelteKit, Nuxt, Remix, Astro, etc.), wants to run headless Chrome without binary size limits, needs persistent browser sessions across commands, or wants ephemeral isolated browser environments. Triggers include \"Vercel Sandbox browser\", \"microVM Chrome\", \"agent-browser in sandbox\", \"browser automation on Vercel\", or any task requiring Chrome in a Vercel Sandbox.","https:\u002F\u002Fgithub.com\u002FYYC-Cube\u002FYYC3-Knowledge-Base\u002Fblob\u002Fmain\u002FYYC3-WebSpec-Web标准\u002FYYC3-WebSpec-Web标准-standards\u002Fbrowser-agent\u002Fskills\u002Fvercel-sandbox\u002FSKILL.md",[],[],{"skillNo":56,"name":57,"nameZh":6,"description":58,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":59,"categories":60,"keywords":61,"publishedAt":19},"01M7CTD8D2BNJY54S65M47HEJA","slack","Interact with Slack workspaces using browser automation. Use when the user needs to check unread channels, navigate Slack, send messages, extract data, find information, search conversations, or automate any Slack task. Triggers include \"check my Slack\", \"what channels have unreads\", \"send a message to\", \"search Slack for\", \"extract from Slack\", \"find who said\", or any task requiring programmatic Slack interaction.","https:\u002F\u002Fgithub.com\u002FYYC-Cube\u002FYYC3-Knowledge-Base\u002Fblob\u002Fmain\u002FYYC3-WebSpec-Web标准\u002FYYC3-WebSpec-Web标准-standards\u002Fbrowser-agent\u002Fskills\u002Fslack\u002FSKILL.md",[],[],{"skillNo":63,"name":64,"nameZh":6,"description":65,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":66,"categories":67,"keywords":68,"publishedAt":19},"01M7CTCSSB8ZP3Q58C3YZDZK1W","electron","Automate Electron desktop apps (VS Code, Slack, Discord, Figma, Notion, Spotify, etc.) using agent-browser via Chrome DevTools Protocol. Use when the user needs to interact with an Electron app, automate a desktop app, connect to a running app, control a native app, or test an Electron application. Triggers include \"automate Slack app\", \"control VS Code\", \"interact with Discord app\", \"test this Electron app\", \"connect to desktop app\", or any task requiring automation of a native Electron application.","https:\u002F\u002Fgithub.com\u002FYYC-Cube\u002FYYC3-Knowledge-Base\u002Fblob\u002Fmain\u002FYYC3-WebSpec-Web标准\u002FYYC3-WebSpec-Web标准-standards\u002Fbrowser-agent\u002Fskills\u002Felectron\u002FSKILL.md",[],[],{"skillNo":70,"name":71,"nameZh":6,"description":72,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":73,"categories":74,"keywords":75,"publishedAt":19},"01M7CTCBJY76688V4DCZGWFHGQ","dogfood","Systematically explore and test a web application to find bugs, UX issues, and other problems. Use when asked to \"dogfood\", \"QA\", \"exploratory test\", \"find issues\", \"bug hunt\", \"test this app\u002Fsite\u002Fplatform\", or review the quality of a web application. Produces a structured report with full reproduction evidence -- step-by-step screenshots, repro videos, and detailed repro steps for every issue -- so findings can be handed directly to the responsible teams.","https:\u002F\u002Fgithub.com\u002FYYC-Cube\u002FYYC3-Knowledge-Base\u002Fblob\u002Fmain\u002FYYC3-WebSpec-Web标准\u002FYYC3-WebSpec-Web标准-standards\u002Fbrowser-agent\u002Fskills\u002Fdogfood\u002FSKILL.md",[],[],{"skillNo":77,"name":78,"nameZh":6,"description":79,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":80,"categories":81,"keywords":82,"publishedAt":19},"01M7CTC4H0FFGVEAQR9PA4GA1A","agent-browser","Browser automation CLI for AI agents. Use when the user needs to interact with websites, including navigating pages, filling forms, clicking buttons, taking screenshots, extracting data, testing web apps, or automating any browser task. Triggers include requests to \"open a website\", \"fill out a form\", \"click a button\", \"take a screenshot\", \"scrape data from a page\", \"test this web app\", \"login to a site\", \"automate browser actions\", or any task requiring programmatic web interaction.","https:\u002F\u002Fgithub.com\u002FYYC-Cube\u002FYYC3-Knowledge-Base\u002Fblob\u002Fmain\u002FYYC3-WebSpec-Web标准\u002FYYC3-WebSpec-Web标准-standards\u002Fbrowser-agent\u002Fskills\u002Fagent-browser\u002FSKILL.md",[],[],{"skillNo":84,"name":85,"nameZh":6,"description":86,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":87,"categories":88,"keywords":89,"publishedAt":19},"01M7CTBKVFYBG78FDSABSDJTTQ","shadcn","Manages shadcn components and projects — adding, searching, fixing, debugging, styling, and composing UI, including chat interfaces. Provides project context, component docs, and usage examples. Applies when working with shadcn\u002Fui, component registries, presets, --preset codes, or any project with a components.json file. Also triggers for \"shadcn init\", \"create an app with --preset\", \"switch to --preset\", and for building a design system or applying a DESIGN.md or brand spec to shadcn\u002Fui.","https:\u002F\u002Fgithub.com\u002FYYC-Cube\u002FYYC3-Knowledge-Base\u002Fblob\u002Fmain\u002FYYC3-UI-界面组件\u002FYYC3-UI-界面组件-ui\u002Fskills\u002Fshadcn\u002FSKILL.md",[],[]]