Play的执行上下文与Scala Global [英] 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演员可以共享同一线程.
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 Global的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!