agentleFS
jscott3201jscott3201/yet-more-skills/skills/python-threading-workers/SKILL.md

python-threading-workers

Skill4 starsChanged 2 months agoApache-2.0View on GitHub

Design or debug Python thread/process workers, shared-state synchronization, free-threaded Python behavior, and native thread-pool budgets. Not asyncio-only scheduling or PyO3 module auditing.

The file

We split this skill into the rules it gives an agent

3 rules. About 0 are common, 3 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈3

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

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

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Keep lock order consistent.

    line 15
  2. Distinctive:

    Create native clients, runtimes, connections, and locks inside workers unless a specific cross-process transfer is supported.

    line 23
  3. Distinctive:

    Read worker checks for version and platform considerations.

    line 31

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “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

Python Threads and Workers

Identify the actual interpreter, GIL runtime state, process start method, native dependencies, and source of parallelism. Decide whether the work is I/O-bound, Python CPU-bound, or native CPU-bound before choosing threads or processes. Existing pools are part of the budget; more workers are not inherently faster.

Shared state and ownership

Protect application invariants across compound operations, not just individual dict/list accesses. Do not use historical GIL behavior or built-in container locks as a transaction guarantee. Prefer explicit ownership, immutable snapshots, queues, or narrow locks. A frozen wrapper does not freeze its contained list or native handle.

Keep lock order consistent. Avoid calling user callbacks while holding non-reentrant locks; snapshot required state and invoke callbacks outside the critical section. Bound submissions, memory, and outstanding results. Define what happens to queued work during cancellation or failure.

For free-threaded execution audit module globals, caches, lazy initialization, iterators, context propagation, and third-party native state. Check the GIL after relevant imports. Separate same-object thread safety from independent-object parallelism. Do not declare subinterpreter support from free-threading evidence; interpreter isolation and module state are different requirements.

Processes and native runtimes

Do not assume fork is the default or a safe shortcut. Inspect the selected platform and Python version. Allow callers to provide an appropriate multiprocessing context instead of globally changing a library user's start method. Worker entry functions and payloads must meet the selected context's serialization/import requirements.

Create native clients, runtimes, connections, and locks inside workers unless a specific cross-process transfer is supported. Do not fork an initialized Tokio or other multithreaded runtime and reuse its inherited handle. Guard application entry points and avoid spawning pools at import time.

Cancellation of an executor Future does not necessarily stop a running function. Provide cooperative cancellation for long work where required; an externally bounded subprocess may be needed for a hard operational boundary. Own worker shutdown and do not rely on daemon threads to flush durable work.

Evidence

Use barriers/events with bounded joins for race tests and fresh processes for hang tests. Stress same-instance and separate-instance access, failure propagation, overload, and repeated create/close. Test the actual GIL-disabled configuration and native host combinations promised by the change. Report CPU/thread counts and untested interpreter isolation claims.

Read worker checks for version and platform considerations.

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.