Skip to content

Instantly share code, notes, and snippets.

@aoiroaoino
Last active April 14, 2022 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aoiroaoino/13024799f22d51c73267d8d862a773b2 to your computer and use it in GitHub Desktop.
Save aoiroaoino/13024799f22d51c73267d8d862a773b2 to your computer and use it in GitHub Desktop.
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