scala - 调度示例不工作

标签 scala scala-dispatch

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

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

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

val res = country()
println(res)

需要帮助。

完整程序-

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)
  }
}

最佳答案

嗯,这里不确定,但也许问题是你的主线程完成得太快了,后台线程(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

当然,在实际程序中你永远不需要这样做。

延迟的另一种选择是在 main 的末尾使用 readLine()

关于scala - 调度示例不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14749743/

相关文章:

scala - 未能初始化编译器 : object java. lang.Object in compiler mirror not found

scala - 环境 monad 中术语环境的含义

java - 为什么 dispatch 会针对特定 URL 抛出 "java.net.ConnectException: General SSLEngine ..."和 "unexpected status"异常?

scala - 使用 Scala 调度库禁用 SSL

scala - databinder 调度无法正确处理 HTTP 302 和身份验证

scala - scala中的 curry 函数

class - 在示例中阅读 Scala,试图理解示例背后的哲学

scala - 如何在 Dispatch for Scala 中使用 DELETE 请求?

scala - 排序 `Future` s 超时

scala - 从本地构建的可变映射返回不可变映射的首选方法是什么?