简体   繁体   English

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

[英]Subprocess.Popen route child process stdout,stderr to parent process

I'd like to call a background task using subprocess.Popen that will run a server on the background, that suppose to print some meaningful messages upon some events like incoming http message.我想使用subprocess.Popen调用后台任务,它将在后台运行服务器,假设在某些事件(如传入的 http 消息)上打印一些有意义的消息。

I want to get those logs and merge them with the parent process so I could observe the whole flow, which include other components besides this server.我想获取这些日志并将它们与父进程合并,以便我可以观察整个流程,其中包括除此服务器之外的其他组件。

According to documentation根据文档

PIPE indicates that a new pipe to the child should be created. PIPE 表示应该创建一个到孩子的新管道。

So I've tried the follow call, but failed to trace the child process logs.所以我尝试了跟随调用,但未能跟踪子进程日志。

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

Any idea how to fix it ?知道如何解决吗?

Try removing all the options and only provide the script to the subprocess.尝试删除所有选项,只向子进程提供脚本。 It should display all logs for the child process in the same terminal.它应该在同一个终端中显示子进程的所有日志。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM