scala – Play Framework 2.0控制器/ Async的工作原理如何?
我最近搬到了Play框架2.0,有一些关于控制器实际工作的问题。
在play docs提到:
但在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个角色分配给控制器处理。我猜每个请求都会在请求的一生中分配其中一个actor。是对的吗? 而且,并行性因素意味着什么以及fork-join-executor与线程池的区别呢? 另外 – docs应该说Async应该用于长时间的计算。什么资格作为长期计算? 100ms的? 300ms的? 5秒? 10秒?我的猜测会是一秒钟以上,但如何确定呢? 这个质询的原因是测试异步控制器调用比常规调用更难。你必须启动一个假的应用程序,并做一个完整的请求,而不是调用一个方法并检查其返回值。 即使不是这样,我怀疑在Async和Akka.future中包装所有内容都是这样。 我在#playframework IRC频道中要求这个,但没有回答,似乎我不是唯一一个不知道应该做什么的事情。 只是重申: >每个请求是否从/ actions池分配一个actor是否正确? 提前致谢。 编辑: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 ;) 解决方法
>当一个消息到达演员一个演员时,只要它需要处理该消息,它就会保持该actor。如果您同步处理请求(在处理该消息期间计算整个响应),则此响应完成之前,此actor无法服务其他请求。如果相反,您可以在接收到此请求后将工作发送给另一个演员,接收请求的演员可以在其他演员正在处理第一个请求时开始下一个请求。
>用于actors的线程数是“num cpus * parallelism-factor”(你可以指定min和max) >邓诺 >除非有真正的计算,否则我会倾向于将任何与其他系统进行通信的异步信息,例如使用数据库/文件系统来执行io。当然可以阻止线程的任何东西。但是,由于传递消息的开销很少,所以我不认为只要把所有的工作发送给其他的actor就会有问题。 >请参阅 Play Documentation on functional tests关于如何测试您的控制器。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |