我正在关注 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/