Last active
April 14, 2022 07:50
-
-
Save aoiroaoino/13024799f22d51c73267d8d862a773b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.concurrent.Future | |
import scala.concurrent.duration._ | |
import java.util.concurrent.Executors | |
implicit val ec = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(5)) | |
val r = Future { | |
val ids = (0 to 10) | |
println(ids) | |
ids.toSeq | |
}.flatMap { | |
_.sliding(3, 3).foldLeft(Future.successful(Seq.empty[Int])) { (acc, ids) => | |
var p = util.Random.alphanumeric.take(3).mkString | |
acc.flatMap { rs => | |
Future.traverse(ids) { id => | |
Future { | |
Thread.sleep(3 * 1000) | |
println(s"${Thread.currentThread.getId} - $p - $id") | |
id * 10 | |
} | |
}.map(rs ++ _) | |
} | |
} | |
} | |
Await.result(r, 15.seconds) | |
def traverse[A, B](in: Seq[A], window: Int)(fn: A => Future[B])(implicit ec: ExecutionContext): Future[Seq[B]] = | |
in.sliding(window, window).foldLeft(Future.successful(Seq.empty[B])) { (acc, is) => | |
acc.flatMap { rs => | |
println("=========") | |
Future.traverse(is)(fn).map(rs ++ _) | |
} | |
} | |
def run(p: Int) = Await.result( | |
traverse((0 to 10).toSeq, p) { i => | |
Future { | |
Thread.sleep(3 * 1000) | |
println(s"${Thread.currentThread.getId} - $i") | |
i * 10 | |
} | |
}, | |
Duration.Inf | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment