1

I have the following piece of code:

stringList map copyURLToFile // List[String]

def copyURLToFile(symbol: String) = {
  val url = new URL(base.replace("XXX", symbol))
  val file = new File(prefix + symbol + ".csv")
  org.apache.commons.io.FileUtils.copyURLToFile(url, file)
}

How do I make copyURLToFile execute in parallel?

Scala Futures need a return type, but if I make copyURLToFile of type Future[Unit], what and how do I return from the function?

5
  • 1
    possible duplicate of Parallel map operations?
    – senia
    Commented Nov 22, 2013 at 11:15
  • You could just add future between = and {like this: def copyURLToFile(symbol: String) = future{
    – senia
    Commented Nov 22, 2013 at 11:17
  • I tried having function body in Future {} and recursively going through the list, but it didn't work. Recursion worked but calls to org.apache.commons.io.FileUtils.copyURLToFile didn't (no files were created). I guess the problem was that Java method returns void.
    – krl
    Commented Nov 22, 2013 at 13:21
  • 1
    Actually the issue was probably that I was executing that as a standalone program whose main thread terminated before worker threads could execute as described here stackoverflow.com/questions/10565475/…
    – krl
    Commented Nov 22, 2013 at 22:31
  • Void is not a problem per se, the problem is that your main thread does not access the result, thus it doesn't wait for it. Commented Mar 9, 2014 at 12:14

2 Answers 2

3

If you want a very easy way to parallelize this, then just map over the list using the parallel collections library.

stringList.par.map(copyURLToFile).toList
1
  • Works great. Much faster now.
    – krl
    Commented Nov 22, 2013 at 13:17
0

You need to return something, how are you going to know which files where downloaded?

Change your code to be:

def copyURLToFile(symbol: String): (String,String) = {
  val url = new URL(base.replace("XXX", symbol))
  val file = new File(prefix + symbol + ".csv")
  org.apache.commons.io.FileUtils.copyURLToFile(url, file)
  return (symbol, file)
}

And then the resulting collection will contain the symbols an the paths to the file where they were downloaded.

1
  • The bigger problem is that the code does not wait for the download to complete, because it does not try to access the results. Commented Mar 9, 2014 at 12:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.