Subprocess.popen() 不适用于 Swift

Subprocess.popen() Doesn't Work With Swift

我想 subprocess.popen() 一个 Swift 程序 Python 3.

parent.py:

import subprocess

#p = subprocess.Popen(['python3', 'sub.py'], universal_newlines = True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.DEVNULL, bufsize=1)

p = subprocess.Popen(['swift', 'sub.swift'], universal_newlines = True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.DEVNULL, bufsize=1)

while True:
    a=input('command:')
    if not a:
        print(p.stdout.readline())
    else:
        p.stdin.write(a+'\n')

sub.swift

while true {
    print(readLine()!)
}

没用。 p.stdout.readline() 摊位。

如果我将命令更改为 ['python3', 'sub.py'] 并且

sub.py

while True:
    print(input())

有效:

> python3 parent.py
command:a
command:[enter]
a

command:asdf
command:[enter]
asdf

这是为什么?我该如何解决?

刷新标准输出对我有用:

import Darwin
while true {
    print(readLine()!)
    fflush(stdout)
}

我对Python不太熟悉,但我的猜测是Python print可能会自动刷新。