我试图创建一个执行环境/ 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