[英]Printing to stdout in subprocess
我有一個腳本,該腳本按如下方式運行子流程:
child_process = subprocess.Popen(["python", testset['dir'] + testname, \
output_spec_file, plugin_directory],\
stderr=subprocess.PIPE, stdout=subprocess.PIPE)
在此過程中,我嘗試插入打印語句,但它們似乎沒有顯示在stdout中。 我嘗試在該子sys.stduout.read()
使用sys.stdout.write()
,然后在child_process
之后child_process
sys.stduout.read()
,但它沒有捕獲輸出。
我是Python的新手,我還沒有達到Python的那種復雜程度。 我實際上在C語言中工作較低,並且有一些Python測試腳本,而且我不確定如何從子進程中打印出來。
有什么建議么?
sys.stdout.read
(和write
)用於當前進程 (而不是子進程 )的標准輸入/輸出。 如果要寫入子進程的stdin,則需要使用:
child_process.stdin.write("this goes to child") #Popen(..., stdin=subprocess.PIPE)
從孩子的stdout流中讀取內容類似:
child_process = subprocess.Popen( ... , stdout=subprocess.PIPE)
child_process.stdout.read("This is the data that comes back")
當然,通常使用起來更慣用:
stdoutdata, stderrdata = child_process.communicate(stdindata)
(注意通過subprocess.PIPE
到POPEN構造適當的地方)提供的輸入數據,可以一次全部通過。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.