Stream stdout from scala Process

Question:

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)
Asked By: nfsquake

||

Answers:

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

var cmd = "python -u python_test.py"
Answered By: atline
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.