I've a lot of doubts related to scala future and how it works, basically I was reading this article about scala async using futures
Play Framework: async I/O without the thread pool and callback hell http://engineering.linkedin.com/play/play-framework-async-io-without-thread-pool-and-callback-hell
the author talk about how hard is handle async programming using thread pools..and explain how async futures works in scala, but this article generates me more questions:
I thought than scala async worked using a thread pool, is it evented async similar to how nodejs works?
I did a small test building a slow server which wait 1 second before response..then (using scala version 2.11.0-M5 and futures from scala.concurrent._ ) I did 100 request to the server
the result are obvious, it tooks a bit more than 100 seconds in complete all the request... now the async code
implicit val executorService = Executors.newFixedThreadPool(8) //I also test without fixing the thread pool
implicit val executionContext = ExecutionContext.fromExecutorService(executorService)
val myFuture = future{
blocking{
. ..long request here
})}
}
myFuture.onComplete{
case Success(_) => ....
case _ => ....
}
I have understood than if I don't fix my thread pool in my executorService, scala based in how many cores has my pc defines how many threads must to take...
running this..the complete test tooks "100/8" seconds aprox, is ok but is not what I would expect from an evented system, seems more related to thread pool where my threads blocks, is it ok??..is what would you expect?
blocking is not avoiding than my code blocks and I don't know the reasons..
my question so far are:
1) what happen if I run scala futures with only a thread?..Would expect a similar behaviour to nodejs, which runs in a single thread and is evented async?...in my dummy test scala async scale based in how many threads I defined previously (so basically it doesnt avoid the thread pool hell)
2) In this answer from Heather Miller
Asynchronous IO in Scala with futures
she gives the impression than when I've a blocking code inside, scala spawn a new worker preventing starvation , but in my example seems not works as expected and my code finally blocks, what am I doing wrong?...
3) What could be the reasons of why my code blocks?...are there some rules I should know when use promises/futures?
4) What is the difference between scala futures,akka futures and twitter futures?..Could I have a total different results based in which tool I choose?...
NOTICE: I fix the thread pool because I was running the sample in a single core architecture (vertx.io)..without it I got not results (scala took only a single thread and it blocks in every request)
NOTICE 2 I'm not so familiar with threads execution and probably I did several mistakes in the terminology than I used...