2011-04-21 91 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()