Python3 从子进程读取标准输出

时间:2021-04-13 18:09:47

标签: python-3.x subprocess

我想启动一个进程作为子进程。 python 脚本仍应从外部获取控制台输入以控制子进程发生的情况。为此,我想定期将子进程的输出打印到控制台,然后读取控制台输入。这是我目前的代码:

def start():
    child_proccess = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)

def check():
    print("some tutorial string")
    
    lines = []

    for line in io.TextIOWrapper(child_proccess.stdout, encoding="utf-8"):
        lines.append(line)

    for line in lines[-10:]:
        print(line)

start()
while True:
    check()
    time.sleep(1)

这里的问题是,通过 stdout 进行迭代会以某种方式阻塞,这意味着第二个 for 循环永远不会执行并且控制台保持空白。

0 个答案:

没有答案
相关问题