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(),t1delegate web request thread (worker thread)w1, go the next line.t1skip contents of function passedmapmethod, since depends on non-blocking i/o call has not yet completed. once t1 returnsasyncresult, moves on process other requests.later on, worker thread
w1finished web request "http://example.com", sends signal listener threadt2, may or may not samet1.t2starts executeresponsefuture.mapline, , delegate task worker threadw2. once task delegatedw2,t2moves on process other requests.later on, worker thread
w2finished creating result (theresponsefuture.mapline), sends signal listener threadt3. ,t3resultw2, send result client in magic way (it looks magic because i've no idea howt3knows 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