使用python子进程从无限运行的程序中读取

时间:2018-09-30 01:59:58

标签: python terminal subprocess

我是python的新手,我想从事子流程。 我想在一个终端中运行python程序,并在另一终端中执行输出时在另一终端中逐行显示输出。 我创建了一个无限for循环。作为第一个程序。

这是我的第二个获取输出的程序。我在python中使用子进程,但是我没有得到实时输出,因为第二个程序仅在第一个程序执行完毕后才执行。

import subprocess
from time import sleep
import sys
# run the shell as a subprocess:
p = subprocess.Popen(['python','/Users/salauddinali/Desktop/temp.py'],stdout = subprocess.PIPE, bufsize=1)
# let the shell output the result:
sleep(0.1)
for c in p.stdout.read():
        n = c.decode('UTF-8') 
        print (n)

0 个答案:

没有答案