scala - 调度示例不起作用

Shw*_*nka 3 scala scala-dispatch

我正在关注调度文档中的第一个示例-

    val svc = url("http://api.hostip.info/country.php")
    val country = Http(svc OK as.String)
    for (c <- country)
      println(c)
Run Code Online (Sandbox Code Playgroud)

我没有打印任何输出.当我将其更改为下面以进行阻塞调用时,我得到输出.

val res = country()
println(res)
Run Code Online (Sandbox Code Playgroud)

需要帮助.

完整的程序 -

import dispatch._
object DispatchTest {

  def main(args: Array[String]) {
    val svc = url("http://api.hostip.info/country.php")
    val country = Http(svc OK as.String)
    for (c <- country)
      println(c)
  }
}
Run Code Online (Sandbox Code Playgroud)

NIA*_*NIA 6

嗯,这里不太确定,但也许问题是你的主线程完成得那么快,后台线程(Dispatch异步工作)没时间采取行动?

要检查这一点,您可以尝试插入延迟:

for (c <- country) // Here we spawn a background thread!
  println(c)

Thread.sleep(500) // So let's wait half a second for it to work
Run Code Online (Sandbox Code Playgroud)

当然,在真正的程序中,你永远不需要这样做.

延迟的另一个选择只是readLine()在主要结束时.