[英]is there a way to print the output to stdout and capture as well?
我正在執行大約15分鍾的命令,我通過fetchPipe.communicate
捕獲了輸出,然后將其打印出來。 Python中是否有辦法將輸出打印到stdout並捕獲?
fetchPipe = subprocess.Popen(cmd.split(' '), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, error) = fetchPipe.communicate()
是。 例如,可以使用偵聽器功能:寫入stdout並在收到行時觸發一些操作
import sys
import subprocess
import threading
聽眾
def listener(proc):
for line in proc.stdout:
sys.stdout.write(line)
triggerRecvLine(line)
proc.wait()
在其他線程中運行偵聽器可能是明智的。
proc=subprocess.Popen(cmd.split(' '), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
t = threading.Thread(target=listener, args=(proc, ))
t.start()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.