parallel-execution
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseParallel Execution
并行执行
CRITICAL: This skill teaches how to execute multiple tasks simultaneously for maximum efficiency.
关键提示:本技能将教授如何同时执行多个任务以实现最高效率。
The Fundamental Rule
基本规则
ALL runSubagent calls MUST be in a SINGLE function_calls block for true parallelism.
If runSubagent calls are in separate messages, they run SEQUENTIALLY, not in parallel.
所有runSubagent调用必须放在单个function_calls块中才能实现真正的并行。
如果runSubagent调用放在不同的消息中,它们将按顺序执行,而非并行。
Why Parallel Execution Matters
并行执行的重要性
Sequential (SLOW - AVOID)
顺序执行(速度慢 - 避免使用)
plaintext
Message 1: Start Task A
↓ wait for completion
Message 2: Start Task B
↓ wait for completion
Message 3: Start Task C
↓ wait for completion
Total time = A + B + C = 90 seconds (if each takes 30s)plaintext
消息1:启动任务A
↓ 等待完成
消息2:启动任务B
↓ 等待完成
消息3:启动任务C
↓ 等待完成
总耗时 = A + B + C = 90秒(假设每个任务耗时30秒)Parallel (FAST - USE THIS)
并行执行(速度快 - 推荐使用)
plaintext
Message 1: Start Task A ─┐
Start Task B ─┼─ All run simultaneously
Start Task C ─┘
Total time ≈ max(A, B, C) = 30 secondsSpeedup: 3x faster with 3 parallel tasks
plaintext
消息1:启动任务A ─┐
启动任务B ─┼─ 所有任务同时运行
启动任务C ─┘
总耗时 ≈ max(A, B, C) = 30秒提速效果:3个并行任务可实现3倍提速
How to Execute in Parallel
如何实现并行执行
Step 1: Identify Independent Tasks
步骤1:识别独立任务
Tasks are independent when:
- They don't depend on each other's output
- They don't modify the same files
- They can run in any order
任务需满足以下条件才是独立的:
- 彼此不依赖对方的输出
- 不会修改相同的文件
- 执行顺序不影响结果
Step 2: Launch ALL Tasks in ONE Message
步骤2:在单条消息中启动所有任务
Launch all runSubagent calls in a single function_calls block:
xml
<!-- CORRECT: All tasks in single function_calls block = PARALLEL -->
<function_calls>
<invoke name="runSubagent">
<parameter name="description">Analyze authentication module</parameter>
<parameter name="prompt">Review src/auth for security patterns...</parameter>
</invoke>
<invoke name="runSubagent">
<parameter name="description">Analyze API layer</parameter>
<parameter name="prompt">Review src/api for REST best practices...</parameter>
</invoke>
<invoke name="runSubagent">
<parameter name="description">Analyze database layer</parameter>
<parameter name="prompt">Review src/db for query optimization...</parameter>
</invoke>
</function_calls>将所有runSubagent调用放在单个function_calls块中:
xml
<!-- 正确做法:所有任务放在单个function_calls块中 = 并行执行 -->
<function_calls>
<invoke name="runSubagent">
<parameter name="description">分析认证模块</parameter>
<parameter name="prompt">审查src/auth中的安全模式...</parameter>
</invoke>
<invoke name="runSubagent">
<parameter name="description">分析API层</parameter>
<parameter name="prompt">审查src/api中的REST最佳实践...</parameter>
</invoke>
<invoke name="runSubagent">
<parameter name="description">分析数据库层</parameter>
<parameter name="prompt">审查src/db中的查询优化...</parameter>
</invoke>
</function_calls>Step 3: Collect and Synthesize Results
步骤3:收集并综合结果
After all tasks complete, combine their findings into a unified response.
所有任务完成后,将它们的发现整合为统一的响应。
Parallelization Patterns
并行化模式
Pattern 1: Task-Based Parallelization
模式1:基于任务的并行化
When you have N independent tasks, spawn N subagents:
markdown
Implementation Plan:
1. Implement auth module
2. Create API endpoints
3. Add database schema
4. Write unit tests
5. Update documentation
Launch 5 parallel subagents:
├─ Subagent 1: Implement auth module
├─ Subagent 2: Create API endpoints
├─ Subagent 3: Add database schema
├─ Subagent 4: Write unit tests
└─ Subagent 5: Update documentationAll 5 in ONE message!
当你有N个独立任务时,启动N个子Agent:
markdown
实施计划:
1. 实现认证模块
2. 创建API端点
3. 添加数据库架构
4. 编写单元测试
5. 更新文档
启动5个并行子Agent:
├─ 子Agent 1:实现认证模块
├─ 子Agent 2:创建API端点
├─ 子Agent 3:添加数据库架构
├─ 子Agent 4:编写单元测试
└─ 子Agent 5:更新文档所有5个任务都要放在单条消息中!
Pattern 2: Directory-Based Parallelization
模式2:基于目录的并行化
Analyze different directories simultaneously:
markdown
Codebase Structure:
├── src/auth/
├── src/api/
├── src/db/
└── src/ui/
Launch 4 parallel subagents:
├─ Subagent 1: Analyze src/auth
├─ Subagent 2: Analyze src/api
├─ Subagent 3: Analyze src/db
└─ Subagent 4: Analyze src/ui同时分析不同的目录:
markdown
代码库结构:
├── src/auth/
├── src/api/
├── src/db/
└── src/ui/
启动4个并行子Agent:
├─ 子Agent 1:分析src/auth
├─ 子Agent 2:分析src/api
├─ 子Agent 3:分析src/db
└─ 子Agent 4:分析src/uiPattern 3: Perspective-Based Parallelization
模式3:基于视角的并行化
Review from multiple angles at once:
markdown
Code Review Perspectives:
- Security vulnerabilities
- Performance bottlenecks
- Test coverage gaps
- Architecture patterns
Launch 4 parallel subagents:
├─ Subagent 1: Security review
├─ Subagent 2: Performance analysis
├─ Subagent 3: Test coverage review
└─ Subagent 4: Architecture assessment同时从多个角度进行审查:
markdown
代码审查视角:
- 安全漏洞
- 性能瓶颈
- 测试覆盖缺口
- 架构模式
启动4个并行子Agent:
├─ 子Agent 1:安全审查
├─ 子Agent 2:性能分析
├─ 子Agent 3:测试覆盖审查
└─ 子Agent 4:架构评估Pattern 4: Adversarial Verification
模式4:对抗性验证
Use conflicting mandates for thorough review:
markdown
Verification Subagents (all parallel):
├─ Syntax & Type Checker
├─ Test Runner
├─ Lint & Style Checker
├─ Security Scanner
└─ Build Validator
Then (sequential, after above complete):
├─ False Positive Filter
├─ Missing Issues Finder
└─ Context Validator使用相互冲突的要求进行全面审查:
markdown
验证子Agent(全部并行):
├─ 语法与类型检查器
├─ 测试运行器
├─ 代码规范与样式检查器
├─ 安全扫描器
└─ 构建验证器
然后(顺序执行,需等上述任务完成):
├─ 误报过滤器
├─ 遗漏问题查找器
└─ 上下文验证器TodoList Integration
待办事项列表集成
When using parallel execution, mark ALL parallel tasks as simultaneously:
in_progress使用并行执行时,需同时将所有并行任务标记为:
in_progressBefore Launching Parallel Tasks
启动并行任务前
json
{
"todos": [
{ "content": "Analyze auth module", "status": "in_progress", "activeForm": "Analyzing auth module" },
{ "content": "Analyze API layer", "status": "in_progress", "activeForm": "Analyzing API layer" },
{ "content": "Analyze database layer", "status": "in_progress", "activeForm": "Analyzing database layer" },
{ "content": "Synthesize findings", "status": "pending", "activeForm": "Synthesizing findings" }
]
}json
{
"todos": [
{ "content": "分析认证模块", "status": "in_progress", "activeForm": "分析认证模块" },
{ "content": "分析API层", "status": "in_progress", "activeForm": "分析API层" },
{ "content": "分析数据库层", "status": "in_progress", "activeForm": "分析数据库层" },
{ "content": "综合发现结果", "status": "pending", "activeForm": "综合发现结果" }
]
}After Each Task Completes
每个任务完成后
Mark as completed as results come in:
json
{
"todos": [
{ "content": "Analyze auth module", "status": "completed", "activeForm": "Analyzing auth module" },
{ "content": "Analyze API layer", "status": "completed", "activeForm": "Analyzing API layer" },
{ "content": "Analyze database layer", "status": "in_progress", "activeForm": "Analyzing database layer" },
{ "content": "Synthesize findings", "status": "pending", "activeForm": "Synthesizing findings" }
]
}收到结果后标记为已完成:
json
{
"todos": [
{ "content": "分析认证模块", "status": "completed", "activeForm": "分析认证模块" },
{ "content": "分析API层", "status": "completed", "activeForm": "分析API层" },
{ "content": "分析数据库层", "status": "in_progress", "activeForm": "分析数据库层" },
{ "content": "综合发现结果", "status": "pending", "activeForm": "综合发现结果" }
]
}When to Parallelize
何时使用并行
Good Candidates
适合并行的场景
| Scenario | Parallel Approach |
|---|---|
| Multiple independent analyses | One subagent per analysis |
| Multi-file processing | One subagent per file/directory |
| Different review perspectives | One subagent per perspective |
| Multiple independent features | One subagent per feature |
| Exploratory research | Multiple search strategies |
| 场景 | 并行方法 |
|---|---|
| 多个独立分析任务 | 每个分析对应一个子Agent |
| 多文件处理 | 每个文件/目录对应一个子Agent |
| 不同审查视角 | 每个视角对应一个子Agent |
| 多个独立功能开发 | 每个功能对应一个子Agent |
| 探索性研究 | 多种搜索策略并行 |
When NOT to Parallelize
不适合并行的场景
| Scenario | Why Sequential |
|---|---|
| Tasks with dependencies | B needs A's output |
| Same file modifications | Risk of conflicts |
| Sequential workflows | Order matters (commit → push → PR) |
| Shared state | Race conditions |
| Limited resources | Overwhelming the system |
| 场景 | 为何选择顺序执行 |
|---|---|
| 存在依赖关系的任务 | B需要A的输出结果 |
| 修改同一文件 | 存在冲突风险 |
| 顺序工作流 | 执行顺序至关重要(提交 → 推送 → 拉取请求) |
| 共享状态 | 可能出现竞态条件 |
| 资源有限 | 可能导致系统过载 |
Performance Impact
性能影响
| # Parallel Tasks | Sequential Time | Parallel Time | Speedup |
|---|---|---|---|
| 2 | 60s | 30s | 2x |
| 3 | 90s | 30s | 3x |
| 5 | 150s | 30s | 5x |
| 10 | 300s | 30s | 10x |
Assuming each task takes ~30 seconds
| 并行任务数量 | 顺序执行时间 | 并行执行时间 | 提速倍数 |
|---|---|---|---|
| 2 | 60s | 30s | 2x |
| 3 | 90s | 30s | 3x |
| 5 | 150s | 30s | 5x |
| 10 | 300s | 30s | 10x |
假设每个任务耗时约30秒
Common Mistakes
常见错误
Mistake 1: Separate Messages
错误1:使用单独的消息
markdown
WRONG (Sequential):
Message 1: "I'll start analyzing the auth module..."
<invoke name="runSubagent">Analyze auth</invoke>
Message 2: "Now let me analyze the API..."
<invoke name="runSubagent">Analyze API</invoke>
RIGHT (Parallel):
Message 1: "I'll analyze all modules in parallel..."
<function_calls>
<invoke name="runSubagent">Analyze auth</invoke>
<invoke name="runSubagent">Analyze API</invoke>
<invoke name="runSubagent">Analyze DB</invoke>
</function_calls>markdown
错误示例(顺序执行):
消息1:"我将开始分析认证模块..."
<invoke name="runSubagent">分析认证模块</invoke>
消息2:"现在让我分析API..."
<invoke name="runSubagent">分析API</invoke>
正确示例(并行执行):
消息1:"我将并行分析所有模块..."
<function_calls>
<invoke name="runSubagent">分析认证模块</invoke>
<invoke name="runSubagent">分析API</invoke>
<invoke name="runSubagent">分析数据库</invoke>
</function_calls>Mistake 2: Announcing Before Acting
错误2:先宣告再执行
markdown
WRONG:
"I'm going to launch three parallel tasks to analyze the codebase."
[waits for response]
"Now launching the tasks..."
RIGHT:
"Launching three parallel analysis tasks now:"
<function_calls>
<invoke name="runSubagent">...</invoke>
<invoke name="runSubagent">...</invoke>
<invoke name="runSubagent">...</invoke>
</function_calls>markdown
错误示例:
"我将启动三个并行任务来分析代码库。"
[等待响应]
"现在启动任务..."
正确示例:
"现在启动三个并行分析任务:"
<function_calls>
<invoke name="runSubagent">...</invoke>
<invoke name="runSubagent">...</invoke>
<invoke name="runSubagent">...</invoke>
</function_calls>Mistake 3: Forgetting Synthesis
错误3:忘记综合结果
markdown
WRONG:
Just dump all task outputs without integration
RIGHT:
After receiving all results, synthesize:
- Identify common themes
- Resolve contradictions
- Prioritize findings
- Create unified recommendationsmarkdown
错误示例:
直接输出所有任务的结果而不进行整合
正确示例:
收到所有结果后进行综合:
- 识别共同主题
- 解决矛盾点
- 优先处理关键发现
- 创建统一的建议报告Parallel Execution Checklist
并行执行检查清单
Before launching parallel tasks, verify:
- Tasks are truly independent
- No shared file modifications
- No sequential dependencies
- All tasks in SINGLE function_calls block
- TodoList updated with all
in_progress - Synthesis step planned
启动并行任务前,请确认:
- 任务确实是独立的
- 不会修改共享文件
- 不存在顺序依赖关系
- 所有任务都放在单个function_calls块中
- 待办事项列表已更新所有任务
in_progress - 已规划结果综合步骤
Template: Parallel Analysis
模板:并行分析
markdown
undefinedmarkdown
undefinedLaunching Parallel Analysis
启动并行分析
I'm analyzing this codebase from multiple perspectives simultaneously.
我将同时从多个角度分析此代码库。
Parallel Tasks
并行任务
<function_calls>
<invoke name="runSubagent">
<parameter name="description">Security Review</parameter>
<parameter name="prompt">Analyze for security vulnerabilities, focusing on:
- Authentication/authorization
- Input validation
- Secrets handling</parameter> </invoke>
<function_calls>
<invoke name="runSubagent">
<parameter name="description">安全审查</parameter>
<parameter name="prompt">分析安全漏洞,重点关注:
- 认证/授权
- 输入验证
- 密钥处理</parameter> </invoke>
Synthesis (after all complete)
结果综合(所有任务完成后)
[Combine findings into prioritized report]
---[将发现整合为优先级排序的报告]
---Quick Reference
快速参考
markdown
RULE #1:
ALL runSubagent calls in SINGLE function_calls block = PARALLEL
runSubagent calls in SEPARATE messages = SEQUENTIAL
PATTERNS:
Task-based: One subagent per task
Directory-based: One subagent per directory
Perspective-based: One subagent per viewpoint
Adversarial: Multiple competing reviewers
TODOLIST:
Mark ALL parallel tasks as in_progress BEFORE launching
Mark each as completed AFTER receiving results
SPEEDUP:
N parallel tasks ≈ Nx faster
(5 tasks @ 30s each: 150s → 30s)
CHECKLIST:
☐ Tasks independent?
☐ No shared files?
☐ No dependencies?
☐ All in ONE function_calls block?
☐ Synthesis planned?markdown
规则1:
所有runSubagent调用放在单个function_calls块中 = 并行执行
runSubagent调用放在不同消息中 = 顺序执行
模式:
基于任务: 每个任务对应一个子Agent
基于目录: 每个目录对应一个子Agent
基于视角: 每个视角对应一个子Agent
对抗性: 多个相互竞争的审查者
待办事项列表:
启动前将所有并行任务标记为in_progress
收到结果后将每个任务标记为已完成
提速效果:
N个并行任务 ≈ N倍提速
(5个任务各耗时30秒:150秒 → 30秒)
检查清单:
☐ 任务是否独立?
☐ 是否没有共享文件?
☐ 是否没有依赖关系?
☐ 是否都在单个function_calls块中?
☐ 是否已规划结果综合步骤?