2

In addition to the future's execution context provided by Scala:

import scala.concurrent.ExecutionContext.Implicits.global

Play provides another execution context:

import play.api.libs.concurrent.Execution.Implicits.defaultContext

When to use each in Play for Scala?

ps0604
  • 1,227
  • 23
  • 133
  • 330

2 Answers2

2

You can find an answer here:

Play's internal execution context

That question is not complete duplicate but very close, and the answer there cover your question as well.

In a short:

You must not use import scala.concurrent.ExecutionContext.Implicits.global in Play.

Response to the comment

The quote from the answer:

Instead, you would use play.api.libs.concurrent.Execution.Implicits.defaultContext, which uses an ActorSystem.

scala.concurrent.ExecutionContext.Implicits.global is an ExecutionContext defined in the Scala standard library. It is a special ForkJoinPool that using the blocking 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.

Community
  • 1
  • 1
Andriy Kuba
  • 8,093
  • 2
  • 29
  • 46
  • What I understand from the link is that Play's context is used internally in Play and it should not be used. Therefore, I should use Scala's execution context (not the answer you're telling me) – ps0604 Mar 28 '17 at 09:53
  • please, see the update, I am not sure why you understand it differently – Andriy Kuba Mar 28 '17 at 10:10
0

As a general rule, if you need an ExecutionContext inside a method or class, require it as an implicit parameter (Scala) or a normal parameter (Java). Convention is to put this parameter last.

This rule allows the caller/creator to control where/how/when asynchronous effects are evaluated.

The main exception to this rule is when you already have an ExecutionContext and do not wish for the caller/creator to be in control of where the effects are evaluated.

Viktor Klang
  • 26,479
  • 7
  • 51
  • 68