Subprocess.Popen 将子进程 stdout、stderr 路由到父进程

Subprocess.Popen route child process stdout,stderr to parent process

我想使用 subprocess.Popen 调用后台任务,这将 运行 后台服务器,假设在某些事件(例如传入的 http 消息)上打印一些有意义的消息。

我想获取这些日志并将它们与父进程合并,以便我可以观察整个流程,其中包括除此服务器之外的其他组件。

根据文档

PIPE indicates that a new pipe to the child should be created.

所以我尝试了后续调用,但未能跟踪子进程日志。

res = subprocess.Popen("python3 http_server.py", 
                       shell=True,
                       stdin=PIPE, 
                       stdout=PIPE, 
                       stderr=STDOUT, 
                       close_fds=True)

知道如何解决吗?

尝试删除所有选项,只向子进程提供脚本。它应该在同一终端中显示子进程的所有日志。

res = subprocess.Popen("python3 http_server.py")