我最近转向使用Play框架2.0,有一些问题困扰着我,关于控制器在Play中的实际工作方式。
在Play文档中提到:
看起来控制器处理程序有24个演员被分配。我猜每个请求都会为其生命周期分配其中一个演员。这是正确的吗?
另外,
还有 - 文档应该说异步应该用于长时间计算。什么样的计算可以称为长时间计算?100ms?300ms?5秒?10秒?
这些问题的原因是测试异步控制器调用比普通调用要困难得多。您必须启动一个虚拟应用程序并进行完整的请求,而不仅仅是调用方法并检查其返回值。
即使那不是这种情况,我也怀疑在
我已经在#playframework IRC频道中提出了这个问题,但没有得到答案,似乎我不是唯一一个不确定该如何做的人。
再次强调:
1. 每个请求是否从/actions池中分配一个演员? 2.
提前感谢。
在Play文档中提到:
然而,在文档的另一部分中也提到:由于Play 2.0的工作方式,操作代码必须尽可能快(即非阻塞)。
/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-executor
与thread-pool
有何不同?还有 - 文档应该说异步应该用于长时间计算。什么样的计算可以称为长时间计算?100ms?300ms?5秒?10秒?
这些问题的原因是测试异步控制器调用比普通调用要困难得多。您必须启动一个虚拟应用程序并进行完整的请求,而不仅仅是调用方法并检查其返回值。
即使那不是这种情况,我也怀疑在
Async
和Akka.future
中包装所有内容是否正确的方式。我已经在#playframework IRC频道中提出了这个问题,但没有得到答案,似乎我不是唯一一个不确定该如何做的人。
再次强调:
1. 每个请求是否从/actions池中分配一个演员? 2.
parallelism-factor
是什么意思,为什么是1?
3. fork-join-executor
与thread-pool-executor
有何不同?
4. 计算需要多长时间才能被包装在Async
中?
5. 没有启动虚拟应用程序,是否无法测试异步控制器方法?提前感谢。
<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 ;)