Skip to content

Instantly share code, notes, and snippets.

@atamborrino
Last active November 16, 2016 10:38
Show Gist options
  • Save atamborrino/17a6a8ee524a03edf3cc364ee475cb50 to your computer and use it in GitHub Desktop.
Save atamborrino/17a6a8ee524a03edf3cc364ee475cb50 to your computer and use it in GitHub Desktop.
Simple parallel composition of Future with max parallelism in a sliding window
import akka.stream.Materializer
import akka.stream.scaladsl._
import scala.concurrent.{Future, ExecutionContext}
// This will execute futures in parallel with a max parallelism
def traverse[A, B](as: Seq[A], parallelism: Int)(f: A => Future[B])
(implicit ec: ExecutionContext, mat: Materializer): Future[Seq[B]] = {
Source(as.toList)
.mapAsync(parallelism)(f)
.runWith(Sink.seq)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment