我試圖創建一個執行環境/ shell,它將在服務器上遠程執行,它通過套接字將stdout,err流式傳輸到瀏覽器中。我目前已經嘗試了使用subprocess.run
和PIPE
的方法。問題是我在完成過程後得到stdout。我想實現的是獲得一行一行,僞終端的實現。使用python的pty創建一個實時控制檯
我當前實現
test.py
def greeter():
for _ in range(10):
print('hello world')
greeter()
,並在外殼
>>> import subprocess
>>> result = subprocess.run(['python3', 'test.py'], stdout=subprocess.PIPE)
>>> print(result.stdout.decode('utf-8'))
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
如果我嘗試用pty
嘗試連這個簡單的實現,如何做一個呢?
檢查了這一點:https://stackoverflow.com/questions/1606795/catching-stdout-in-realtime-from-subprocess –
嘗試使用'bufsize = 1'參數子處理設置行緩衝區,並使用iter (result.stdout.readline,b'')'讀取包裝的stdout,而True循環 – Vinny