3

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

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

2 回答 2

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

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

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

于 2013-02-07T11:40:30.800 回答
0

它在这里工作:

scala> import dispatch._
import dispatch._

scala> val svc = url("http://api.hostip.info/country.php")
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
svc: com.ning.http.client.RequestBuilder = com.ning.http.client.RequestBuilder@2f823290

scala> val country = Http(svc OK as.String)
country: dispatch.Promise[String] = Promise(-incomplete-)

scala> for (c <- country)
     |   println(c)

scala> BR

注意BR出现在提示之后的那个。

您确定该国家/地区没有印在某处,但您没有注意到是因为它与其他输出混淆了吗?

于 2013-02-07T11:27:19.447 回答