从 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(time) ,这样我只等待一段时间。
我相信他们应该是一个简单的解决方案,但我找不到它......
My code has to call some external programs which sometimes hangs. (endless loop, will never return)
To start the external Process i use:
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
But i didn't find a way to check if the process is still running. Or a waitFor(time) so that i only wait for some time.
I believe their should be a simple solution but i'm not able to find it...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,
Process
中的方法exitValue
定义如下:因此您可以检查
exitValue()
是否返回None< /code> 或
Some
值。None
表示进程仍在运行。它遵循 文档JavaProcess.exitValue()
As far as I can see method
exitValue
inProcess
is defined as folows:So you can check whether
exitValue()
returnsNone
orSome
value.None
means that process is still running. It follows from documentation to JavaProcess.exitValue()