I am new to play framework and scala in general. While trying to test and understand the differences between sync and async actions, using the following code:
package controllers
import play.api.mvc._
import play.api.libs.concurrent.Execution.Implicits._
import scala.concurrent.Future
object Application extends Controller {
def async = Action.async {
Logger.info("async start")
val resultF = Future {
Thread.sleep(2000)
Logger.info("async end")
Ok
}
Logger.info("non-blocking")
resultF
}
def sync = Action {
Thread.sleep(2000)
Ok
}
}
When running the application, I have 10 tabs in browser requesting "/async". My expectation was all request should take roughly 2 seconds to furfill and I will see in the log 10 "async start" entries followed by 10 "async end" entries.
However, the actual outcome was seeing "async start", "async end" 10 times. The next request did not start until the previous request has finished. It seems execution of async was blocking and could not handle concurrent requests at all.
My question is why does the system behave this way, and what specific changes to enable concurrent request handling.