- 論壇徽章:
- 0
|
本帖最后由 wn0112 于 2013-05-30 11:09 編輯
jeppeter 發(fā)表于 2013-05-30 10:49 ![]()
回復(fù) 3# wn0112
如果只是 “即時(shí)” 輸出到終端的話(也就是sys.stdout)這樣就可以了,是沒問題的
- import subprocess
- pipe = subprocess.Popen('myapp.exe file.ext')
復(fù)制代碼 但現(xiàn)在是要取得輸出內(nèi)容,就用了管道,除非用readline(),read(), communicate()都不能“即時(shí)”
你是說這樣用嗎?也是不行的,要等myapp.exe結(jié)束才有輸出
- import subprocess, time
- pipe = subprocess.Popen('myapp.exe file.ext', stdout=subprocess.PIPE)
- while 1:
- pipe.stdout.flush()
- pipe.stdout.read()
- time.sleep(0.2)
復(fù)制代碼 如果在read()里設(shè)置字節(jié)數(shù),且字節(jié)數(shù)小于實(shí)際內(nèi)容,也可以即時(shí),但如果大于實(shí)際內(nèi)容也不會(huì)即時(shí)了,好像要等達(dá)到字節(jié)數(shù)或結(jié)束才輸出
- import subprocess, time
- pipe = subprocess.Popen('myapp.exe file.ext', stdout=subprocess.PIPE)
- while 1:
- pipe.stdout.read(5)
- time.sleep(0.2)
復(fù)制代碼 |
|