threading

Reviews Python threading, multiprocessing, and concurrent.futures code for thread-safety and parallelism patterns.

Updated Sep 2, 2026
One-click install
npx skills add https://github.com/Dazlarus/karl-code --skill threading-dazlarus
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: threading
Source: https://github.com/Dazlarus/karl-code/tree/main/.agents/skills/threading
Command: npx skills add https://github.com/Dazlarus/karl-code --skill threading-dazlarus

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing correct concurrent Python code is error-prone: shared state without locks causes race conditions, inconsistent lock ordering causes deadlocks, and choosing threads for CPU-bound work wastes the GIL. This Skill provides review guidance and proven patterns for safe parallel code. ## Core Features & Use Cases - Executor Selection Guidance: Directs ThreadPoolExecutor for I/O-bound tasks and ProcessPoolExecutor for CPU-bound work. - Thread-Safety Patterns: Provides lock-protected counters, Queue-based producer-consumer designs, and deadlock prevention via consistent lock ordering. - Structured Review Output: Produces a threading review listing issues with line references, positive patterns, and recommendations. - Use Case: When reviewing a worker module that increments a shared counter from multiple threads, the Skill flags the unprotected increment and recommends a threading.Lock or Queue-based redesign. ## Quick Start Review my worker.py file for thread-safety issues, race conditions, and deadlock risks using the threading skill.

Frequently Asked Questions about threading

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I choose between threading and multiprocessing in Python?

Use ThreadPoolExecutor for I/O-bound tasks like network requests, since threads release the GIL during I/O waits. Use ProcessPoolExecutor for CPU-bound work like image processing, because separate processes bypass the GIL and run truly in parallel.

How do I use concurrent.futures ThreadPoolExecutor?

Create a ThreadPoolExecutor with a max_workers limit, submit tasks with executor.submit, and collect results via as_completed or executor.map. Always handle exceptions from future.result() and use the context manager for clean shutdown.

How do I prevent deadlocks when using multiple locks?

Prevent deadlocks by always acquiring locks in a consistent global order, such as sorting lock holders by object id before acquiring. Also prefer context managers so locks are always released, and avoid holding one lock while blocking on another.

When should I not use threading in Python?

Avoid threading for CPU-bound work, since the GIL prevents parallel execution; use multiprocessing instead. Also skip it for simple sequential tasks where coordination overhead outweighs benefits, and prefer async for high-concurrency I/O.

Why does my shared counter give wrong results across threads?

Increment operations are not atomic, so concurrent threads interleave reads and writes, losing updates. Protect shared state with threading.Lock, or use queue.Queue to pass data between threads instead of sharing mutable variables.