2011-04-21 38 views
4

我的代码必须调用一些有时挂起的外部程序。 (无限循环,将永远不会返回)从Scala中删除挂起的进程

要开始我用的是外部进程:

import tools.nsc.io.Process 
val res = Process("ls") 
res.foreach(println) 
res.waitFor // waits until a Process is finished but if it's hanging waitFor will not return or 
res.destroy // kills a process 

,但我没有找到一个方法来检查,如果该进程仍在运行。或waitFor(时间),以便我只等待一段时间。

我相信他们应该是一个简单的解决方案,但我没能找到它......

回答

3

据我所看到的方法exitValueProcess被定义为folows:

def exitValue(): Option[Int] = 
    catching(classOf[IllegalThreadStateException]) opt process.exitValue() 

因此您可以检查exitValue()是否返回NoneSome的值。 None表示进程仍在运行。它跟从documentation to Java Process.exitValue()