Last active
October 25, 2021 19:39
-
-
Save hohonuuli/7ebfd438f297ef3ee7fc843920936ba9 to your computer and use it in GitHub Desktop.
Scala version of python download code in Medium article
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
#!/usr/bin/env scala | |
/* | |
@author Brian Schlining | |
@since 2021-10-25 | |
Scala version of example code at | |
https://python.plainenglish.io/send-http-requests-as-fast-as-possible-in-python-304134d46604 | |
*/ | |
import java.io.BufferedInputStream | |
import java.net.URL | |
import java.nio.charset.StandardCharsets | |
import java.util.concurrent.Executors | |
import scala.collection.immutable.LazyList | |
import scala.concurrent.Await | |
import scala.concurrent.duration.Duration | |
import scala.concurrent.ExecutionContext | |
import scala.concurrent.Future | |
import scala.util.Failure | |
import scala.util.Success | |
import scala.util.Using | |
implicit val ec = ExecutionContext.fromExecutorService(Executors.newFixedThreadPool(20)) | |
// Future's are executed asynchronously on the implicit ExecutionContext | |
def downloadLink(url: URL): Future[Unit] = Future { | |
val opt = Using(new BufferedInputStream(url.openStream())) { io => { | |
new String(io.readAllBytes(), StandardCharsets.UTF_8) | |
}} | |
// `Using` automatically closes the input stream from the URL. It returns | |
// A Success or Failure. We pattern match to see which was returned. | |
opt match { | |
case Success(s) => println(s"Read ${s.length} from ${url}") | |
case Failure(exception) => println(s"Failed to read from ${url}") | |
} | |
} | |
// Scala version of Python's: url_list = ["https://www.google.com","https://www.bing.com"]*50 | |
val urls = List("https://www.google.com/","https://www.bing.com").map(new URL(_)) | |
val urlList = LazyList.continually(urls) | |
.flatten | |
.take(100) | |
.toList | |
val start = System.nanoTime() | |
Await.ready(Future.sequence(urlList.map(downloadLink)), Duration.Inf) | |
val end = System.nanoTime() | |
println(s"download ${urlList.length} links in ${(end - start) / 1000000000.0} seconds") | |
// If the ExecutionContext is not closed, the script won't exit. | |
ec.shutdown() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment