2012-09-25 65 views
16

我最近转移到了Play框架2.0,并且关于控制器实际上如何工作的一些问题。Play框架2.0控制器/ Async究竟如何工作?

play docs可以举出:

由于的方式播放2.0作品,动作代码必须尽可能快 可能的(即,非阻挡)。

但是在another part of the docs

  /actions { 
       router = round-robin 
       nr-of-instances = 24 
      } 

 actions-dispatcher = { 
      fork-join-executor { 
       parallelism-factor = 1.0 
       parallelism-max = 24 
      } 
     } 

似乎有分配给控制器处理24个演员。我想每个请求都会在请求的一生中分配这些角色中的一个。 是吗?

另外,parallelism-factor是什么意思?fork-join-executorthread-pool有什么不同?

另外 - 文档应该说,异步应该用于长时间的计算。 什么资格作为长期计算? 100ms的? 300ms的? 5秒? 10秒?我的猜测会超过一秒钟,但如何确定?

此质疑的原因是测试异步控制器调用比常规调用困难。你必须启动一个虚假的应用程序并做一个完整的请求,而不是只调用一个方法并检查它的返回值。

即使情况并非如此,我怀疑包装AsyncAkka.future的一切都是如此。

我在#playframework IRC频道中询问过这个问题,但没有答案,似乎我不是唯一不知道该怎么做的人。

只是重申:

  1. 是不是每一个请求分配从/动作池的一个演员?
  2. parallelism-factor是什么意思?为什么是1?
  3. fork-join-executorthread-pool-executor有什么不同?
  4. 应计算多长时间才能包装在Async
  5. 不可能测试异步控制器方法而不旋转假应用程序?

在此先感谢。

编辑:从IRC

从IRC一些东西,一些东西。

<imeredith> arturaz: i cant be boethered writing up a full reply but here are key points 
<imeredith> arturaz: i believe that some type of CPS goes on with async stuff which frees up request threads 
<arturaz> CPS? 
<imeredith> continuations 
<imeredith> when the future is finished, or timedout, it then resumes the request 
<imeredith> and returns data 
<imeredith> arturaz: as for testing, you can do .await on the future and it will block until the data is ready 
<imeredith> (i believe) 
<imeredith> arturaz: as for "long" and parallelism - the longer you hold a request thread, the more parrellism you need 
<imeredith> arturaz: ie servlets typically need a lot of threads because you have to hold the request thread open for a longer time then if you are using play async 
<imeredith> "Is it right that every request allocates one actor from /actions pool?" - yes i belive so 
<imeredith> "What does parallelism-factor mean and why is it 1?" - im guessing this is how many actors there are in the pool? 
<imeredith> or not 
<imeredith> "How does fork-join-executor differ from thread-pool-executor?" -no idea 
<imeredith> "How long should a calculation be to become wrapped in Async?" - i think that is the same as asking "how long is a piece of string" 
<imeredith> "Is is not possible to test async controller method without spinning up fake applications?" i think you should be able to get the result 
<viktorklang> imeredith: A good idea is to read the documentation: http://doc.akka.io/docs/akka/2.0.3/general/configuration.html (which says parallelism-factor is: # Parallelism (threads) ... ceil(available processors * factor)) 
<arturaz> viktorklang, don't get me wrong, but that's the problem - this is not documentation, it's a reminder to yourself. 
<arturaz> I have absolutely no idea what that should mean 
<viktorklang> arturaz: It's the number of processors available multiplied with the factor you give, and then rounded up using "ceil". I don't know how it could be more clear. 
<arturaz> viktorklang, how about: This factor is used in calculation `ceil(number of processors * factor)` which describes how big is a thread pool given for your actors. 
<viktorklang> arturaz: But that is not strictly true since the size is also guarded by your min and max values 
<arturaz> then why is it there? :) 
<viktorklang> arturaz: Parallelism (threads) ... ceil(available processors * factor) could be expanded by adding a big of conversational fluff: Parallelism (in other words: number of threads), it is calculated using the given factor as: ceil(available processors * factor) 
<viktorklang> arturaz: Because your program might not work with a parallelism less than X and you don't want to use more threads than X (i.e if you have a 48 core box and you have 4.0 as factor that'll be a crapload of threads) 
<viktorklang> arturaz: I.e. scheduling overhead gives diminishing returns, especially if ctz switching is across physical slots. 
<viktorklang> arturaz: Changing thread pool sizes will always require you to have at least basic understanding on Threads and thread scheduling 
<viktorklang> arturaz: makes sense? 
<arturaz> yes 
<arturaz> and thank you 
<arturaz> I'll add this to my question, but this kind of knowledge would be awesome docs ;) 

回答

6
  1. 当消息演员到达演员,它只要它需要处理该消息保存到该演员。如果您同步处理请求(在处理该消息期间计算整个响应),则该响应完成之前,此actor不能处理其他请求。如果您可以在收到此请求后将作品发送给其他演员,则收到请求的演员可以在其他演员正在处理第一个请求时开始处理下一个请求。

  2. 用于演员线程的数量是“数的CPU *并行因子”(可以但指定的最小值和最大值)

  3. 说不上

  4. 除非有真正的计算回事,我'倾向于使任何与其他系统交谈的东西异步,就像用数据库/文件系统做io一样。当然,任何可能阻止线程的东西。然而,由于传递消息的开销很小,我不认为将所有的工作交给其他参与者会有问题。

  5. 查看Play Documentation on functional tests关于如何测试您的控制器。

+0

关于点1.什么是发送任务了别的演员,而不是仅仅增加线程数的效益/动作喜欢150 (用于150个并发动作)? – arturaz

+0

这样想想吧。你的桌上有数以千计的事情要做。哪个会更有效率?从堆中取出一个,在其上工作,直到完成,然后在下一个工作。或者拿走他们的第一百五十名,每人在150种不同的东西之间分配你的时间。第一种效率更高,因为您不会浪费时间在“上下文切换”上。这里同样如此。 – stew

+0

但是,将任务发送给另一个参与者也会导致上下文切换。有什么好处? –

1

看来,你可以测试这样做:

object ControllerHelpers { 
    class ResultExtensions(result: Result) { 
    /** 
    * Retrieve Promise[Result] from AsyncResult 
    * @return 
    */ 
    def asyncResult = result match { 
     case async: AsyncResult => async.result 
     case _ => throw new IllegalArgumentException(
     "%s of type %s is not AsyncResult!".format(result, result.getClass) 
    ) 
    } 

    /** 
    * Block until result is available. 
    * 
    * @return 
    */ 
    def await = asyncResult.await 

    /** 
    * Block until result is available. 
    * 
    * @param timeout 
    * @return 
    */ 
    def await(timeout: Long) = asyncResult.await(timeout) 

    /** 
    * Block for max 5 seconds to retrieve result. 
    * @return 
    */ 
    def get = await.get 
    } 
} 

    implicit def extendResult(result: Result) = 
    new ControllerHelpers.ResultExtensions(result) 


    val result = c.postcodeTimesCsv()(request(params)).get 
    status(result) should be === OK