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)
Tell python not to buffer your output in scala with -u
will help you:
var cmd = "python -u python_test.py"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With