agentleFS
tim-osterhustim-osterhus/millrace-skills/skills/async-python-patterns/SKILL.md

async-python-patterns

Skill0 starsChanged 7 months agoApache-2.0View on GitHub

Guardrail for Python runtime loops that must choose sync-first or asyncio with owned task lifetimes, cancellation, shutdown, and blocking-call isolation.

19 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use docs-ci, docs-interfaces and 10 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • jh941213Skill

    docs-ci

    문서 CI(링크·OpenAPI lint·breaking 게이트·mermaid 검증)나 문서 드리프트 감지(docs.yaml 신선도 검사)를 설치하거나 돌릴 때 사용. 문서 내용 작성은 docs-architecture·docs-interfaces·docs-manuals.

    Also in 3 projects that use this

  • jh941213Skill

    docs-interfaces

    API·이벤트 인터페이스 문서(OpenAPI·swagger·AsyncAPI 명세, 엔드포인트 레퍼런스, API 구성도·시퀀스, API CHANGELOG)를 만들거나 코드와 맞출 때 사용. 시스템 구성도는 docs-architecture, API 설계 원칙은 api-design-principles.

    Also in 3 projects that use this

  • Aver005Skill

    python-resource-management

    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.

    Also in 4 projects that use this

  • CodeWithBehnamSkill

    python-type-safety

    Python type safety with type hints, generics, protocols, and strict type checking. Use when adding type annotations, implementing generic classes, defining structural interfaces, or configuring mypy/pyright.

    Also in 4 projects that use this

  • xfstudioSkill

    python-testing-patterns

    Implement comprehensive testing strategies with pytest, fixtures, mocking, and test-driven development. Use when writing Python tests, setting up test suites, or implementing testing best practices.

    Also in 11 projects that use this

  • RachaelQuiselSkill

    client-docs

    Client documentation. Use when a build needs documentation updates or missing project documents need to be identified.

    Also in 3 projects that use this

  • +4 more from jh941213

The file

We split this skill into the rules it gives an agent

12 rules. 0 are common, 10 are distinctive, and 2 are very specific to this project.

  • 0

    Common

    Most of the 17 edited versions we read keep these.

  • 10

    Distinctive

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Start sync-first: name the coordination or responsiveness problem that async must solve.

    line 40
  2. Very specific:

    Use structured ownership such as asyncio.TaskGroup or an explicit task registry with observed exceptions.

    line 42
  3. Distinctive:

    Treat cancellation as normal control flow and make cleanup idempotent.

    line 44
  4. Distinctive:

    Verify timeout, cancellation, slow callback, and shutdown behavior with targeted tests or a repeatable smoke run.

    line 45
  5. Very specific:

    Do not add async by default.

    line 48
  6. Distinctive:

    Do not spawn naked background tasks.

    line 49
  7. Distinctive:

    Do not hold locks or mutable state guards across awaits unless the state model explicitly permits it.

    line 50
  8. Distinctive:

    Do not block an event loop with filesystem polling, subprocess waits, SDK calls, sleep loops, or CPU-heavy work.

    line 51
  9. Distinctive:

    Keep shutdown ordered: stop intake, drain or abandon by policy, cancel remaining work, observe exceptions, then exit.

    line 53
  10. Distinctive:

    Add diagnostics that expose slow callbacks, never-awaited coroutines, and never-retrieved exceptions.

    line 76
  11. Distinctive:

    Start with the single runtime boundary in scope.

    line 88
  12. Distinctive:

    Run targeted tests or a smoke harness for success, timeout, cancellation, and shutdown behavior.

    line 95

“Common” is measured: kept by at least 6 in 10 of the 17 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Async Python Patterns

Purpose

Help agents decide whether a Python runtime component should stay synchronous or adopt asyncio, then implement async only where it solves a concrete coordination problem. This skill is for service loops, supervisors, queues, subprocess boundaries, cancellation, timeouts, and graceful shutdown.

Use When

  • adding or repairing an engine supervisor, queue worker, adapter loop, file watcher, or runtime service
  • shutdown hangs, leaked background tasks, swallowed exceptions, or timeout behavior need diagnosis
  • blocking file I/O, SDK calls, or subprocess calls appear on an event-loop path
  • a component must coordinate multiple waits without freezing the process

Do Not Use When

  • a simple synchronous loop is fast enough and easier to reason about
  • the task is framework-specific FastAPI, Trio, AnyIO, Celery, or web-handler work
  • the desired behavior is a fire-and-forget task with no owner or shutdown contract

Quick Start

  1. Start sync-first: name the coordination or responsiveness problem that async must solve.
  2. Identify the owner scope for every task, queue, subprocess, watcher, and cancellation signal.
  3. Use structured ownership such as asyncio.TaskGroup or an explicit task registry with observed exceptions.
  4. Move blocking calls behind asyncio.to_thread(), executor boundaries, asyncio subprocess APIs, or keep the component synchronous.
  5. Treat cancellation as normal control flow and make cleanup idempotent.
  6. Verify timeout, cancellation, slow callback, and shutdown behavior with targeted tests or a repeatable smoke run.

Operating Constraints

  • Do not add async by default; async needs a specific concurrency reason.
  • Do not spawn naked background tasks. Every task must be awaited, cancelled, or supervised by an owner.
  • Do not hold locks or mutable state guards across awaits unless the state model explicitly permits it.
  • Do not block an event loop with filesystem polling, subprocess waits, SDK calls, sleep loops, or CPU-heavy work.
  • Apply config and persisted-state reloads only at explicit boundaries.
  • Keep shutdown ordered: stop intake, drain or abandon by policy, cancel remaining work, observe exceptions, then exit.
  • Log task names, work identifiers, timeout boundaries, and cancellation paths where operators need diagnosis.

Inputs This Skill Expects

  • The runtime entrypoint, supervisor loop, worker, watcher, or adapter file.
  • The shutdown contract: signal handling, stop marker, parent lifecycle, or operator command.
  • The Python version floor and allowed async primitives.
  • The repo validation path: tests, smoke command, or daemon run command.
  • Any logs showing hangs, leaked tasks, missed cancellations, or blocking behavior.

Output Contract

  • Either recommend sync-first with minimal async changes, or justify a bounded async design.
  • Name the owner for every task and the exit path for every long-running operation.
  • State queue handoff, backpressure, timeout, cancellation, and blocking-call isolation policy.
  • Include targeted tests or smoke evidence for timeout, cancellation, and shutdown.
  • Report any compatibility downgrade if Python version constraints prevent modern primitives.

Procedure

  1. Confirm the runtime boundary and whether async is justified.
  2. Locate task owners, queue boundaries, subprocess calls, sleeps, file I/O, and SDK calls.
  3. Design the smallest concurrency model that solves the real problem.
  4. Implement ownership, cancellation, timeout, and shutdown paths before adding new concurrent work.
  5. Move blocking work off the event loop or keep the surrounding component synchronous.
  6. Add diagnostics that expose slow callbacks, never-awaited coroutines, and never-retrieved exceptions.
  7. Validate the normal path plus timeout, cancellation, and shutdown paths.

Pitfalls And Gotchas

  • Sprinkling async across a codebase without changing ownership or shutdown behavior.
  • Creating tasks without retaining handles or observing exceptions.
  • Treating CancelledError as an unexpected failure instead of a normal stop path.
  • Mixing sync file/subprocess calls into an event-loop path.
  • Letting reloads mutate active state in the middle of a stage or work item.
  • Claiming shutdown is fixed without testing a live cancellation or stop sequence.

Progressive Disclosure

Start with the single runtime boundary in scope. Expand into subprocess supervision, queue backpressure, config reloads, diagnostics, or daemon lifecycle only when the current component crosses those boundaries. Prefer tests over broad architecture essays.

Verification Pattern

  • Confirm async is either rejected with a sync-first reason or accepted with a concrete coordination reason.
  • Confirm every spawned task has an owner and observed exception path.
  • Confirm event-loop paths do not call blocking APIs directly.
  • Confirm shutdown stops intake, drains or abandons by policy, cancels remaining tasks, and exits.
  • Run targeted tests or a smoke harness for success, timeout, cancellation, and shutdown behavior.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.