Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stream stdout from scala Process

Tags:

scala

I am using scala Process to kick off a python program and using ProcessLogger to capture the stdout from the python program. I see that the print statements in the python program are printed only after the python program completes. Is there a way to stream the python print statements as they are executed ?

import scala.sys.process.{ProcessLogger, _}

object TestProcessStdOut {
  def main(args: Array[String]) {
    var cmd = "python python_test.py";
    val process = Process(cmd).run(new ProcessLogger {
      override def out(s: => String) = println(s)

      override def buffer[T](f: => T) = ???

      override def err(s: => String) = ???
    })
  }
}

python_test.py

import time

print("print data 1")
time.sleep(2)
print("print data 2")
time.sleep(2)
print("print data 3")
time.sleep(2)
like image 827
nfsquake Avatar asked Mar 17 '17 18:03

nfsquake


1 Answers

Tell python not to buffer your output in scala with -u will help you:

var cmd = "python -u python_test.py"
like image 159
atline Avatar answered Sep 21 '22 16:09

atline