如何运行系统命令并在Jupyter Notebook中获得连续输出?

How to run system command and get continuous output in Jupyter Notebook?

如果我在 Jupyter Notebook 中 运行 系统命令,我希望我的控制台应用程序的日志消息显示在笔记本中 "immediately"。但是,它们似乎只在整个过程完成后才会发生。

我试过了

!D:/long_running_executable_with_log_messages.exe

import os
os.system('D:/long_running_executable_with_log_messages.exe')

=>如何连续获取系统命令的输出?

相关问题:

from subprocess import Popen, PIPE, CalledProcessError

with Popen(cmd, stdout=PIPE, bufsize=1, universal_newlines=True) as p:
    for line in p.stdout:
        print(line, end='') # process line here

if p.returncode != 0:
    raise CalledProcessError(p.returncode, p.args)

发现于Constantly print Subprocess output while process is running