1. 程式人生 > >python subprocess pipe 實時輸出日誌

python subprocess pipe 實時輸出日誌

* test11.py

import time
print "1"
time.sleep(2)
print "1"
time.sleep(2)
print "1"
time.sleep(2)
print "1"

* test.py

import subprocess


p = subprocess.Popen("python test11.py", shell=True, stdout=subprocess.PIPE)
# None表示正在執行中
while p.poll() is None: 
  out = p.stdout.readline()
  if out != "":
    print out