How does the execution context from
import scala.concurrent.ExecutionContext.Implicits.global
differ from Play's execution contexts:
import play.core.Execution.Implicits.{internalContext, defaultContext}
How does the execution context from
import scala.concurrent.ExecutionContext.Implicits.global
differ from Play's execution contexts:
import play.core.Execution.Implicits.{internalContext, defaultContext}
They are very different.
In Play 2.3.x and prior,
play.core.Execution.Implicits.internalContext
is aForkJoinPool
with fixed constraints on size, used internally by Play. You should never use it for your application code. From the docs:Instead, you would use
play.api.libs.concurrent.Execution.Implicits.defaultContext
, which uses anActorSystem
.In 2.4.x, they both use the same
ActorSystem
. This means that Akka will distribute work among its own pool of threads, but in a way that is invisible to you (other than configuration). Several Akka actors can share the same thread.scala.concurrent.ExecutionContext.Implicits.global
is anExecutionContext
defined in the Scala standard library. It is a specialForkJoinPool
that using theblocking
method to handle potentially blocking code in order to spawn new threads in the pool. You really shouldn't use this in a Play application, as Play will have no control over it. It also has the potential to spawn a lot of threads and use a ton of memory, if you're not careful.I've written more about
scala.concurrent.ExecutionContext.Implicits.global
in this answer.They are the same and point out to the default dispatcher of the underlying actor system in your Play or Akka or combined application.
Default Play's Context
Play's Internal Context
Guice's EC Injected
But this is different:
Also DB drivers, e.g. if you use slick, may come up with their own Execution Context. Anyway,
Best Practices:
scala.concurrent.ExecutionContext.Implicits.global
, when you are in play or akka framework, in this way you may use more threads than optimum during high load so the performance may decrease.scala.concurrent.ExecutionContext.Implicits.global
when you have no other executor running in your app. Don’t worry this is safe then.Await
for a futureapplication.conf