effect-concurrency-fibers

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Concurrency & 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
    fork
    for background loops; always manage interruption
  • Prefer
    Effect.all
    for independent operations
  • Use
    Effect.forEach
    with
    concurrency
    for pools
  • Combine with retries and timeouts for resilient parallelism
  • 限制并发数以保护资源
  • 对后台循环使用
    fork
    ;务必管理中断
  • 对于独立操作,优先使用
    Effect.all
  • 对资源池使用带
    concurrency
    参数的
    Effect.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

相关链接

Local Source Reference

本地源码参考

CRITICAL: Search local Effect source before implementing
The full Effect source code is available at
docs/effect-source/
. Always search the actual implementation before writing Effect code.
重要提示:实现前先搜索本地Effect源码
完整的Effect源码位于
docs/effect-source/
。编写Effect代码前,务必先查看实际的实现。

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
undefined
bash
undefined

Find 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
undefined
grep -F "race" docs/effect-source/effect/src/Effect.ts grep -F "timeout" docs/effect-source/effect/src/Effect.ts
undefined

Workflow

工作流程

  1. Identify the concurrency API you need (e.g., Effect.all, fork)
  2. Search
    docs/effect-source/effect/src/Effect.ts
    for the implementation
  3. Study the types and concurrency options
  4. Look at test files for usage examples
  5. Write your code based on real implementations
Real source code > documentation > assumptions
  1. 确定所需的并发API(如Effect.all、fork)
  2. docs/effect-source/effect/src/Effect.ts
    中搜索其实现
  3. 研究类型与并发选项
  4. 查看测试文件中的使用示例
  5. 基于真实实现编写代码
真实源码 > 文档 > 假设

References

参考资料