Play 的执行上下文与 Scala 全局 [英] Play's execution contexts vs scala global
问题描述
执行上下文如何来自
import scala.concurrent.ExecutionContext.Implicits.global
与 Play 的执行上下文不同:
differ from Play's execution contexts:
import play.core.Execution.Implicits.{internalContext, defaultContext}
推荐答案
它们非常不同.
在 Play 2.3.x 及更早版本中,play.core.Execution.Implicits.internalContext
是一个 ForkJoinPool
,对大小有固定限制,供 Play 内部使用.您永远不应该将它用于您的应用程序代码.来自文档:
In Play 2.3.x and prior, play.core.Execution.Implicits.internalContext
is a ForkJoinPool
with fixed constraints on size, used internally by Play. You should never use it for your application code. From the docs:
Play 内部线程池 - 由 Play 在内部使用.此线程池中的线程不应执行任何应用程序代码,也不应在此线程池中进行任何阻塞.其大小可通过在application.conf中设置internal-threadpool-size进行配置,默认为可用处理器数.
Play Internal Thread Pool - This is used internally by Play. No application code should ever be executed by a thread in this thread pool, and no blocking should ever be done in this thread pool. Its size can be configured by setting internal-threadpool-size in application.conf, and it defaults to the number of available processors.
相反,您可以使用 play.api.libs.concurrent.Execution.Implicits.defaultContext
,它使用一个 ActorSystem
.
Instead, you would use play.api.libs.concurrent.Execution.Implicits.defaultContext
, which uses an ActorSystem
.
在 2.4.x 中,它们都使用相同的 ActorSystem
.这意味着 Akka 将在其自己的线程池中分配工作,但以一种对您不可见的方式(配置除外).多个 Akka actor 可以共享同一个线程.
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
是 Scala 标准库中定义的 ExecutionContext
.这是一个特殊的 ForkJoinPool
,它使用 blocking
方法来处理潜在的阻塞代码,以便在池中生成新线程.您真的不应该在 Play 应用程序中使用它,因为 Play 无法控制它.如果您不小心,它还有可能产生很多线程并使用大量内存.
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.
我在 这个答案.
I've written more about scala.concurrent.ExecutionContext.Implicits.global
in this answer.
这篇关于Play 的执行上下文与 Scala 全局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!