gpt4 book ai didi

scala - Scala Worker Actors 的设计注意事项

转载 作者:行者123 更新时间:2023-12-01 12:51:24 25 4
gpt4 key购买 nike

今年秋天我将参加分布式系统类(class),由于 Scala 参与者在这个领域非常有用,我才刚刚开始学习 Scala。到目前为止,我从文本“Scala 中的 Actors”中读到关于 Actors 的内容,我有一个问题似乎没有在文本中非常清楚地解决。

我目前正在一台配备多核处理器的机器上为我的程序建模,并在 Master-Workers 模型中使用基于事件的参与者。现在,以下两种设计选择中的哪一种会导致所有处理器内核得到最佳使用?

1) 将 actors 的数量限制在一个固定的小数目,不管 master Actor 有多少工作,都要分发给 worker actors(顺便说一句,有时可能相当大)

2) 生成与 Master Actor 需要完成的工作数量一样多的 actor

哪些因素会影响我对上述两个选择的决定?

更新:我打印出我正在运行的 100 个 worker actors 的线程 ID,这是输出 -

No. of Processors-2
MainActor is on thread:Thread[ForkJoinPool-1-worker-0,5,main]
AccumulatorActor is on thread:Thread[ForkJoinPool-1-worker-1,5,main]

Worker Actor-1 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-4 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-5 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-6 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-7 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-8 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-9 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-10 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-3 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-12 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-13 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-14 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-15 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-2 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-16 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-11 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-17 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-18 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-19 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-20 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-21 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-22 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-23 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-24 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-25 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-26 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-28 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-29 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-27 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-30 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-31 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-33 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-32 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-34 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-35 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-36 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-38 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-39 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-40 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-41 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-42 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-43 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-44 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-45 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-46 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-47 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-48 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-51 running on thread:Thread[ForkJoinPool-1-worker-1,5,main]
Worker Actor-50 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-54 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-49 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-56 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-57 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-58 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-55 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-59 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-52 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-61 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-60 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-63 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-53 running on thread:Thread[ForkJoinPool-1-worker-1,5,main]
Worker Actor-65 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-66 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-68 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-64 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-62 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-70 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-69 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-67 running on thread:Thread[ForkJoinPool-1-worker-1,5,main]
Worker Actor-72 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-73 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-71 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-75 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-78 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-79 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-80 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-81 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-82 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-83 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-74 running on thread:Thread[ForkJoinPool-1-worker-1,5,main]
Worker Actor-84 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-85 running on thread:Thread[ForkJoinPool-1-worker-1,5,main]
Worker Actor-77 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-76 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-86 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-87 running on thread:Thread[ForkJoinPool-1-worker-1,5,main]
Worker Actor-88 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-89 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-91 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-92 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-94 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-95 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-96 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-90 running on thread:Thread[ForkJoinPool-1-worker-1,5,main]
Worker Actor-98 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-99 running on thread:Thread[ForkJoinPool-1-worker-1,5,main]
Worker Actor-100 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-93 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-37 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-97 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]

如上所示,ForkJoinPool 中有四个工作线程在运行,而我的机器上有 2 个 cpu 内核。我不确定为什么会这样,因为快速查看 ResizableThreadPoolScheduler 的来源让我相信最初池中工作线程的数量等于 cpu 核心的数量,并且这些线程会动态增加以防任何参与者被池中的一个工作线程阻塞。我使用的所有参与者都是基于事件的( react )并且不执行任何阻塞操作。一个理想的场景是在 ForkJoinPool 中只有 2 个线程,并让它们在执行每个参与者工作的两个核心上并行运行。我对此的理解是正确的还是我在这里遗漏了什么?

最佳答案

您听起来像是在将 actor 视为线程。拥有 Actor 系统的部分目的是将您的问题与 Actor 本身分离。 Actor 的创作成本很低,所以拥有几千个 Actor 就可以了。拥有成千上万的 Actor 并不意味着他们每个人都会尝试同时运行。

Scheduler是管理如何执行参与者请求的。默认情况下,你会得到一个 ForkJoinScheduler或者(在特殊的 JVM 上) ResizableThreadPoolScheduler

并行任务的数量由 ThreadPoolConfig 控制在这两种情况下,默认为 max(2*cores,4)。在此类中,您还可以了解如何通过系统属性覆盖这些默认值:

  • actors.enableForkJoin 允许您控制调度程序的选择
  • actors.corePoolSize 设置初始“线程”池大小
  • actors.maxPoolSize 设置最大尺寸

请注意,研究 Akka actor 可能是一个更好的主意,因为它们很可能 replace the original Scala actor implementation在某些时候,提供更多功能并强制更严格地遵守 actor 模型(因为您只得到一个 ActorRef 并且不能再直接调用 actor 上的方法)。在 Akka 中,调度器的等价物是 dispatchers .

关于scala - Scala Worker Actors 的设计注意事项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12251293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com