Last active
November 16, 2016 10:38
-
-
Save atamborrino/17a6a8ee524a03edf3cc364ee475cb50 to your computer and use it in GitHub Desktop.
Simple parallel composition of Future with max parallelism in a sliding window
This file contains hidden or 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 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