python-concurrency-performance

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Python Concurrency and Performance

Python并发与性能

Overview

概述

Correct concurrency starts with matching the model to the workload, not the developer's preference. This skill encodes defaults for model selection, cancellation/deadline behavior, and lifecycle safety—prioritizing explicit control over implicit magic.
Treat these recommendations as preferred defaults. When project constraints demand deviation, call out tradeoffs and compensating controls.
正确的并发实现始于让模型匹配工作负载,而非开发者的偏好。 本技能规定了模型选择、取消/截止时间行为以及生命周期安全性的默认准则——优先选择显式控制而非隐式的“魔法”实现。 请将这些建议视为首选默认方案。当项目约束要求偏离时,需明确说明权衡取舍和补偿控制措施。

When to Use

适用场景

  • Selecting between
    asyncio
    ,
    threading
    ,
    multiprocessing
    , or
    concurrent.futures
  • Propagating deadlines or cancellation through async call chains
  • Bounding fan-out, backpressure, or semaphore-guarded concurrency
  • Diagnosing race conditions, deadlocks, or priority inversion
  • Profiling throughput bottlenecks before and after optimization
  • Verifying no task or thread leaks on shutdown or lifecycle transitions
  • asyncio
    threading
    multiprocessing
    concurrent.futures
    之间选择并发模型
  • 在异步调用链中传播截止时间或取消操作
  • 限制扇出、背压或使用信号量保护的并发
  • 诊断竞争条件、死锁或优先级反转问题
  • 在优化前后分析吞吐量瓶颈
  • 验证在关闭或生命周期转换时是否存在任务或线程泄漏

When NOT to Use

不适用场景

  • Pure CPU-bound numeric work better served by NumPy/C extensions
  • Single-threaded scripting with no concurrent I/O
  • Distributed systems coordination (use a workflow/orchestration skill instead)
  • 纯CPU密集型数值计算任务(更适合用NumPy/C扩展)
  • 无并发I/O的单线程脚本
  • 分布式系统协调(请改用工作流/编排类技能)

Quick Reference

快速参考

  • Choose the concurrency model by workload profile (I/O-bound → asyncio/threads; CPU-bound → multiprocessing).
  • Keep cancellation and cleanup explicit—never rely on garbage collection to close resources.
  • Bound fan-out and backpressure with semaphores or queue limits; unbounded spawning invites OOM.
  • Measure before optimizing; re-measure after every change to confirm the win.
  • Verify no task/thread leaks on any lifecycle-sensitive change (startup, shutdown, reconnect).
  • 根据工作负载配置文件选择并发模型(I/O密集型→asyncio/线程;CPU密集型→多进程)。
  • 保持取消操作和清理的显式性——绝不依赖垃圾回收来关闭资源。
  • 使用信号量或队列限制来约束扇出和背压;无限制地生成任务会引发内存不足(OOM)。
  • 先测量再优化;每次变更后重新测量以确认优化效果。
  • 在任何对生命周期敏感的变更(启动、关闭、重新连接)后,验证是否存在任务/线程泄漏。

Common Mistakes

常见错误

  • Defaulting to threads for I/O-bound work
    asyncio
    avoids thread-safety bugs entirely for network I/O; threads add synchronization overhead for no gain.
  • Ignoring cancellation propagation — a cancelled parent that doesn't cancel children leaks tasks and holds connections open.
  • Unbounded
    gather
    /
    submit
    calls
    — spawning thousands of tasks without a semaphore or bounded executor starves the event loop or exhausts OS threads.
  • Optimizing without profiling — guessing at bottlenecks leads to complex code that solves the wrong problem; always profile first.
  • Missing shutdown verification — tests that don't assert clean shutdown mask slow resource leaks that surface only in production under load.
  • 默认使用线程处理I/O密集型工作——对于网络I/O,
    asyncio
    可完全避免线程安全问题;线程只会增加同步开销而无任何收益。
  • 忽略取消操作的传播——已取消的父任务若不取消子任务,会导致任务泄漏并保持连接打开。
  • 无限制地调用
    gather
    /
    submit
    ——在不使用信号量或有界执行器的情况下生成数千个任务,会耗尽事件循环或操作系统线程。
  • 未做分析就优化——猜测瓶颈会导致代码复杂却解决了错误的问题;始终要先做性能分析。
  • 未验证关闭过程——未断言干净关闭的测试会掩盖仅在生产环境负载下才会显现的缓慢资源泄漏。

Scope Note

范围说明

  • Treat these recommendations as preferred defaults for common cases, not universal rules.
  • If a default conflicts with project constraints or worsens the outcome, suggest a better-fit alternative and explain why it is better for this case.
  • When deviating, call out tradeoffs and compensating controls (tests, observability, migration, rollback).
  • 请将这些建议视为常见场景的首选默认方案,而非通用规则。
  • 如果默认方案与项目约束冲突或导致更差的结果,请提出更合适的替代方案并解释为何更适合当前场景。
  • 当偏离默认方案时,需明确说明权衡取舍和补偿控制措施(测试、可观测性、迁移、回滚)。

Invocation Notice

调用说明

  • Inform the user when this skill is being invoked by name:
    python-concurrency-performance
    .
  • 当调用本技能时,需告知用户技能名称:
    python-concurrency-performance

References

参考资料

  • references/concurrency-models.md
  • references/deadlines-cancellation-lifecycle.md
  • references/leak-detection.md
  • references/concurrency-models.md
  • references/deadlines-cancellation-lifecycle.md
  • references/leak-detection.md