gpt4 book ai didi

scala - 如何并行执行多个任务?

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

我上课Parallel Programming ,它显示了并行接口(interface):

def parallel[A, B](taskA: => A, taskB: => B): (A, B) = {
val ta = taskA
val tb = task {taskB}
(ta, tb.join())
}

以下是错误的:
def parallel[A, B](taskA: => A, taskB: => B): (A, B) = {
val ta = taskB
val tb = task {taskB}.join()
(ta, tb)
}

更多界面见 https://gist.github.com/ChenZhongPu/fe389d30626626294306264a148bd2aa

它还向我们展示了执行四个任务的正确方法:
def parallel[A, B, C, D](taskA: => A, taskB: => B, taskC: => C, taskD: => D): (A, B, C, D) = {
val ta = task { taskA }
val tb = task { taskB }
val tc = task { taskC }
val td = taskD
(ta.join(), tb.join(), tc.join(), td)
}

我的问题:如果我不知道提前完成的任务数量(任务列表),我该如何调用 join每个任务正确吗?
tasks.map(_.join()) // wrong

编辑

类似的讨论也发生在 Discuss this week's module: Parallel Programming

最佳答案

使用 framework来自 Parallel Programming类(class)

您可以像这样实现该方法:

def parallel[A](tasks: (() => A)*): Seq[A] = {
if (tasks.isEmpty) Nil
else {
val pendingTasks = tasks.tail.map(t => task { t() })
tasks.head() +: pendingTasks.map(_.join())
}
}

(请注意,您是 can't have variable number of by-name arguments - 虽然这是 can change )

然后像这样使用它:
object ParallelUsage {
def main(args: Array[String]) {
val start = System.currentTimeMillis()

// Use a list of tasks:
val tasks = List(longTask _, longTask _, longTask _, longTask _)
val results = parallel(tasks: _*)
println(results)

// or pass any number of individual tasks directly:
println(parallel(longTask, longTask, longTask))
println(parallel(longTask, longTask))
println(parallel(longTask))
println(parallel())

println(s"Done in ${ System.currentTimeMillis() - start } ms")
}

def longTask() = {
println("starting longTask execution")
Thread.sleep(1000)
42 + Math.random
}
}

使用 Scala's parallel collections

你不能比这更简单:
val tasks = Vector(longTask _, longTask _, longTask _)
val results = tasks.par.map(_()).seq

关于scala - 如何并行执行多个任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38712242/

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