如何异步操作API中戏2.2.x的框架斯卡拉的作品? [英] How async Action api works in play framework 2.2.x for scala?
问题描述
我试图创建异步API。但反应显示顺序执行。
步骤来完成:在镀铬的两个选项卡中打开URL。赶紧打他们一个其他的了。网址例如: - 本地主机:9000 / getStar
I was trying to create async api. But the response shows sequential execution.
Steps done: Open the url in two tabs of chrome. And hit them one after other quickly. url ex:- localhost:9000/getStar
.
但执行日志是这样的: -
But the execution log is like :-
[info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:9000
(Server started, use Ctrl+D to stop and go back to the console...)
[success] Compiled in 107ms
[info] application - Application has started
[info] play - Application started (Dev)
[info] application - Async started ************************** :tarun
[info] application - Success Async call :1
[info] application - Success Async call :2
[info] application - Success Async call :3
[info] application - Success Async call :4
[info] application - Success Async call :5
[info] application - Success Async call :6
[info] application - Success Async call :7
[info] application - Success Async call :8
[info] application - Success Async call :9
[info] application - Async finished ************************** :tarun
[info] application - Async started ************************** :tarun1
[info] application - Success Async call :1
[info] application - Success Async call :2
[info] application - Success Async call :3
[info] application - Success Async call :4
[info] application - Success Async call :5
[info] application - Success Async call :6
[info] application - Success Async call :7
[info] application - Success Async call :8
[info] application - Success Async call :9
[info] application - Async finished ************************** :tarun1
在code这个是:
The code for this is :
package controllers
import play.Logger
import play.api.libs.json.Json
import play.api.mvc._
import scala.concurrent.Future
object StarController extends Controller {
import play.api.libs.concurrent.Execution.Implicits.defaultContext
def getStarAsync(name : String) = Action.async{
val futureResult = Future{
Logger.info("Async started ************************** :" + name)
val a = 0;
for( a <- 1 until 10) {
Thread.sleep(1000)
Logger.info("Success Async call :" + a.toString)
}
Logger.info("Async finished ************************** :" + name)
Map("success" -> Json.toJson(true), "msg" -> Json.toJson("Success Async by :" + name), "code" -> Json.toJson(200))
}
futureResult.map{ result =>
Ok(Json.toJson(result))
}
}
}
谁能帮我明白了,为什么的执行顺序是,即使异步调用?
Can anyone please help me understand , why the execution of the was sequential even with async call ?
推荐答案
Action.async
不会奇迹般地使控制器方法异步的。在仅的事情是它不同的是,它需要一个的未来[结果]
代替结果
。而已。控制器是异步的,否则,因为他们可以通过自然(即正常动作
被包裹在一个未来
反正)。这里的事情是,视频下载(1000)
的块的它的线程,是没有一丝异步的。
Action.async
doesn't magically make the controller method asynchronous. The only thing it is different about it is that it expects a Future[Result]
instead of a Result
. That's it. Controllers are otherwise asynchronous as they can be by nature (i.e. a normal Action
gets wrapped in a Future
anyway). The thing here is that Thread.sleep(1000)
blocks it's thread, and is not the least bit asynchronous.
另一件事是,在开发模式(即激活运行
),该剧服务器使用一个线程来服务请求,所以它能够妥善处理重载/编译变阵,等等。所以,有什么情况是,你只是用阻塞同步调用该线程。你应该使用激活启动
看到不同的结果,但即便如此,也没有一点用 Action.async
在这里,除非你是打算委派阻塞到一个不同的线程池。
The other thing is that in dev mode (i.e. activator run
), the play server uses a single thread to serve requests, so it can properly handle reload/compile, evolutions, etc. So what's happening is that you're just blocking that thread with synchronous calls. You should see different results using activator start
, but even so, there's no point in using Action.async
here unless you're going to delegate that blocking to a different thread pool.
<一个href=\"http://stackoverflow.com/questions/23997418/are-there-any-benefits-in-using-non-async-actions-in-play-framework-2-2/24004444#24004444\">Further 阅读。
这篇关于如何异步操作API中戏2.2.x的框架斯卡拉的作品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!