effect-concurrency-fibers
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseConcurrency & Fibers
并发与Fiber
When to use
适用场景
- Parallelizing independent work safely with limits
- Coordinating background tasks and lifecycle
- Racing operations for latency control
- 安全地对独立工作进行并行化并设置限制
- 协调后台任务与生命周期
- 通过竞争操作控制延迟
Parallel Patterns
并行模式
ts
const results = yield* Effect.all(tasks, { concurrency: 10 })ts
const processed = yield* Effect.forEach(items, processItem, { concurrency: 5 })ts
const results = yield* Effect.all(tasks, { concurrency: 10 })ts
const processed = yield* Effect.forEach(items, processItem, { concurrency: 5 })Fiber Lifecycle
Fiber生命周期
ts
const fiber = yield* Effect.fork(work)
const value = yield* Fiber.join(fiber)
yield* Fiber.interrupt(fiber)ts
const fiber = yield* Effect.fork(work)
const value = yield* Fiber.join(fiber)
yield* Fiber.interrupt(fiber)Racing / Timeouts
竞争/超时
ts
const fastest = yield* Effect.race(slow, fast)
const withTimeout = yield* Effect.timeout(operation, "5 seconds")ts
const fastest = yield* Effect.race(slow, fast)
const withTimeout = yield* Effect.timeout(operation, "5 seconds")Guidance
指导原则
- Limit concurrency to protect resources
- Use for background loops; always manage interruption
fork - Prefer for independent operations
Effect.all - Use with
Effect.forEachfor poolsconcurrency - Combine with retries and timeouts for resilient parallelism
- 限制并发数以保护资源
- 对后台循环使用;务必管理中断
fork - 对于独立操作,优先使用
Effect.all - 对资源池使用带参数的
concurrencyEffect.forEach - 结合重试与超时实现弹性并行性
Pitfalls
注意事项
- Unbounded concurrency can exhaust CPU/IO or hit rate limits
- Always interrupt background fibers on shutdown
- Don’t block inside fibers; keep work asynchronous/effectful
- 无限制的并发会耗尽CPU/IO资源或触发速率限制
- 关闭时务必中断后台Fiber
- 不要在Fiber内阻塞;保持工作为异步/有副作用的形式
Cross-links
相关链接
- Errors & Retries: backoff + jitter for transient failures
- Streams & Pipelines: concurrent map over streams
- EffectPatterns inspiration: https://github.com/PaulJPhilp/EffectPatterns
- 错误与重试:针对瞬时故障使用退避+抖动策略
- 流与管道:对流进行并发映射
- EffectPatterns灵感来源:https://github.com/PaulJPhilp/EffectPatterns
Local Source Reference
本地源码参考
CRITICAL: Search local Effect source before implementing
The full Effect source code is available at . Always search the actual implementation before writing Effect code.
docs/effect-source/重要提示:实现前先搜索本地Effect源码
完整的Effect源码位于。编写Effect代码前,务必先查看实际的实现。
docs/effect-source/Key Source Files
关键源码文件
- Effect:
docs/effect-source/effect/src/Effect.ts - Fiber:
docs/effect-source/effect/src/Fiber.ts - Duration:
docs/effect-source/effect/src/Duration.ts
- Effect:
docs/effect-source/effect/src/Effect.ts - Fiber:
docs/effect-source/effect/src/Fiber.ts - Duration:
docs/effect-source/effect/src/Duration.ts
Example Searches
示例搜索
bash
undefinedbash
undefinedFind Effect.all and concurrency patterns
查找Effect.all和并发模式
grep -F "Effect.all" docs/effect-source/effect/src/Effect.ts
grep -F "Effect.all" docs/effect-source/effect/src/Effect.ts
Find forEach with concurrency
查找带concurrency参数的forEach
grep -rF "forEach" docs/effect-source/effect/src/ | grep -F "concurrency"
grep -rF "forEach" docs/effect-source/effect/src/ | grep -F "concurrency"
Study Fiber lifecycle operations
研究Fiber生命周期操作
grep -F "export" docs/effect-source/effect/src/Fiber.ts | grep -E "fork|join|interrupt"
grep -F "export" docs/effect-source/effect/src/Fiber.ts | grep -E "fork|join|interrupt"
Find race and timeout implementations
查找race和timeout的实现
grep -F "race" docs/effect-source/effect/src/Effect.ts
grep -F "timeout" docs/effect-source/effect/src/Effect.ts
undefinedgrep -F "race" docs/effect-source/effect/src/Effect.ts
grep -F "timeout" docs/effect-source/effect/src/Effect.ts
undefinedWorkflow
工作流程
- Identify the concurrency API you need (e.g., Effect.all, fork)
- Search for the implementation
docs/effect-source/effect/src/Effect.ts - Study the types and concurrency options
- Look at test files for usage examples
- Write your code based on real implementations
Real source code > documentation > assumptions
- 确定所需的并发API(如Effect.all、fork)
- 在中搜索其实现
docs/effect-source/effect/src/Effect.ts - 研究类型与并发选项
- 查看测试文件中的使用示例
- 基于真实实现编写代码
真实源码 > 文档 > 假设
References
参考资料
- Agent Skills overview: https://www.anthropic.com/news/skills
- Skills guide: https://docs.claude.com/en/docs/claude-code/skills
- Agent Skills概述:https://www.anthropic.com/news/skills
- Skills指南:https://docs.claude.com/en/docs/claude-code/skills