关于 effect-concurrency-fibers
这项技能介绍如何用 Effect 管理并发,包括并行任务、Fiber 生命周期、竞速和超时,面向编写异步 Effect 代码的开发者,并强调限制并发及实现前查阅本地源码。
主要功能
- 01限制并发运行任务
- 02派生、汇合和中断 Fiber
- 03竞速操作并设置超时
- 04搜索本地 Effect 源码和测试
适用场景
- 01并行执行相互独立的 Effect 操作
- 02管理后台 Fiber 并在关闭时中断
- 03为竞争操作设置延迟上限
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ mepuka/ effect-ontology --skill effect-concurrency-fibers安装后在 Claude Code 中输入/effect-concurrency-fibers确认已加载。
详细介绍
并发与 Fibers
适用场景
- 在限制条件下安全地并行处理独立工作
- 协调后台任务与生命周期
- 通过竞速操作控制延迟
并行模式
const results = yield* Effect.all(tasks, { concurrency: 10 })
const processed = yield* Effect.forEach(items, processItem, { concurrency: 5 })
Fiber 生命周期
const fiber = yield* Effect.fork(work)
const value = yield* Fiber.join(fiber)
yield* Fiber.interrupt(fiber)
竞速 / 超时
const fastest = yield* Effect.race(slow, fast)
const withTimeout = yield* Effect.timeout(operation, "5 seconds")
指南
- 限制并发,以保护资源
- 使用
fork运行后台循环;始终管理中断 - 对独立操作优先使用
Effect.all - 使用带有
concurrency的Effect.forEach创建任务池 - 结合重试和超时,实现可靠的并行处理
常见问题
- 无限制的并发可能耗尽 CPU / IO 资源或触发速率限制
- 关闭时始终中断后台 Fiber
- 不要在 Fiber 中阻塞;保持工作异步或基于 Effect
交叉链接
- 错误与重试:针对瞬时故障的退避 + 抖动
- 流与管道:对流执行并发映射
- EffectPatterns 灵感来源:https://github.com/PaulJPhilp/EffectPatterns
本地源码参考
重要:实现前先搜索本地 Effect 源码
完整的 Effect 源代码位于 docs/effect-source/。编写 Effect 代码前,务必搜索实际实现。
关键源码文件
- Effect:
docs/effect-source/effect/src/Effect.ts - Fiber:
docs/effect-source/effect/src/Fiber.ts - Duration:
docs/effect-source/effect/src/Duration.ts
搜索示例
# Find Effect.all and concurrency patterns
grep -F "Effect.all" docs/effect-source/effect/src/Effect.ts
# Find forEach with concurrency
grep -rF "forEach" docs/effect-source/effect/src/ | grep -F "concurrency"
# Study Fiber lifecycle operations
grep -F "export" docs/effect-source/effect/src/Fiber.ts | grep -E "fork|join|interrupt"
# Find race and timeout implementations
grep -F "race" docs/effect-source/effect/src/Effect.ts
grep -F "timeout" docs/effect-source/effect/src/Effect.ts
工作流
- 确定所需的并发 API(例如 Effect.all、fork)
- 在
docs/effect-source/effect/src/Effect.ts中搜索实现 - 研究类型和并发选项
- 查看测试文件中的用法示例
- 根据实际实现编写代码
实际源码 > 文档 > 假设
参考资料
- Agent Skills 概览:https://www.anthropic.com/news/skills
- Skills 指南:https://docs.claude.com/en/docs/claude-code/skills
评论
支持 Markdown