简体   繁体   中英

When calling Python as a subprocess, can I force it to run in interactive mode?

I'm on a mac using Scala, and I want to create a Python interpreter as a subprocess that my program interacts with. I've been using Process with a ProcessIO, but python insists on running in non-interactive mode. So it only does anything after I close down its input and kill the process. Is there a way to force it to run in interactive mode, so that I can keep the Python process alive and interact with it? This sample code (which I'm pasting into a Scala repl) shows the problem:

import scala.sys.process._
import scala.io._
import java.io._
import scala.concurrent._
val inputStream = new SyncVar[OutputStream];
val process = Process("python").run(pio)
val pio = new ProcessIO(
    (stdin: OutputStream) => {
        inputStream.put(stdin)
    },
    (stdout: InputStream) => {
        while (true) {
            if (stdout.available > 0){
                Source.fromInputStream(stdout).getLines.foreach(println)
            }
        }
    },
    stderr => Source.fromInputStream(stderr).getLines.foreach(println),
    daemonizeThreads=true
    )
def write(s: String): Unit = {
    inputStream.get.write((s + "\n").getBytes)
    inputStream.get.flush()
}
def close(): Unit = {
    inputStream.get.close
}
write("import sys")
write("try: print 'ps1:', sys.ps1")
write("except: print 'no ps1'")
close  // it's only here that output prints to the screen

Invoke Python with the -i flag.

When no script is specified, this causes Python to run in interactive mode whether or not stdin appears to be a terminal. When a script is specified, this causes Python to enter interactive mode after executing the script.

UPDATE: this answer is completely wrong, see discussion in the comments


python already runs in interactive mode.

-i flags is useful if you have to run a script first eg, python -i some_script.py (or -c command). It does not apply in your case.

// it's only here that output prints to the screen

Your issue is the block-buffering mode if the output is not a tty . Pass -u command-line flag to python , to unbuffer its standard streams . Or call sys.stdout.flush() after print .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM