multithreading - In Scala/Playframework, how does "Future" and "Future.map" work under the hood? -
the demo codes here
object proxycontroller extends controller { def proxy = action { val responsefuture: future[response] = ws.url("http://example.com").get() val resultfuture: future[result] = responsefuture.map { resp => // create result uses http status, body, , content-type // example.com response status(resp.status)(resp.body).as(resp.ahcresponse.getcontenttype) } async(resultfuture) } }
as understand, workflow looks this:
one of listener threads (threads process http request),
t1
, executes proxy action, running through code top bottom. when runs @ws.url("http://example.com").get()
,t1
delegate web request thread (worker thread)w1
, go the next line.t1
skip contents of function passedmap
method, since depends on non-blocking i/o call has not yet completed. once t1 returnsasyncresult
, moves on process other requests.later on, worker thread
w1
finished web request "http://example.com", sends signal listener threadt2
, may or may not samet1
.t2
starts executeresponsefuture.map
line, , delegate task worker threadw2
. once task delegatedw2
,t2
moves on process other requests.later on, worker thread
w2
finished creating result (theresponsefuture.map
line), sends signal listener threadt3
. ,t3
resultw2
, send result client in magic way (it looks magic because i've no idea howt3
knows original client.. done closures?)
is real workflow under hood? if so, complex , ineffective thread communication? if not, happened under hood codes above? have ideas this?
Comments
Post a Comment