Scala - getting a callback when an external process exits

前端 未结 3 950
慢半拍i
慢半拍i 2021-01-02 05:08

I\'m looking to replace a lot of my perl with scala. One of the things I tend to do a lot is call binaries (usually compiled C++, but could be java, other perl scripts, q sc

3条回答
  •  梦毁少年i
    2021-01-02 05:41

    You can wait for the end of a process calling exitValue. You might do that in a separate thread, in which the callback will happen. Maybe class Process could be pimped thus:

    import scala.concurrent.ops.spawn
    implicit def ProcessWithCallback(p: Process) {
      def whenTerminatedDo(callback: Int => Unit) = spawn{
        val exitValue = p.exitValue; callback(p)
      }
    }
    

    You could then use that in Exe as you like.

    The Process class given by the JVM and wrapped by scala.sys.Process is really rather feable, it will be hard not to block a thread

提交回复
热议问题