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:

  1. 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 passed map method, since depends on non-blocking i/o call has not yet completed. once t1 returns asyncresult, moves on process other requests.

  2. later on, worker thread w1 finished web request "http://example.com", sends signal listener thread t2, may or may not same t1. t2 starts execute responsefuture.map line, , delegate task worker thread w2. once task delegated w2, t2 moves on process other requests.

  3. later on, worker thread w2 finished creating result (the responsefuture.map line), sends signal listener thread t3. , t3 result w2 , send result client in magic way (it looks magic because i've no idea how t3 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

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -