【发布时间】:2025-12-07 17:35:01
【问题描述】:
我有一个调用fork()的C程序
我有一个 python 脚本,它执行 C 程序
child = subprocess.Popen(command, shell=True, stderr=subprocess.PIPE,stdout=subprocess.PIPE, bufsize=0)
现在我可以使用 child.stderr.read(1) 或 child.communicate() 从 stdout 和 stderr 读取,...但是我现在的问题是,我怎样才能得到 仅 forked 进程的输出。这甚至可能吗?我可以从原始 C 程序和 fork 中获取 pid 吗?
亲切的问候, 非常感谢:)
法比安
【问题讨论】:
-
“这可能吗?”不。“我可以从原始 C 程序和 fork 两者中获取 pid 吗?”不,C 程序拥有那个孩子。您的进程拥有 C 程序。现在。停止担心 Linux 细节并描述您的真正问题。为什么你的 Python 程序不能和 C 程序运行相同的子进程?
-
一种更简单的方法可能是使用不同的输出格式,主进程可以从:“ParentOutput:”开始,而子进程的输出是:“ChildOutput:”。然后你可以在你的python程序中忽略“ParentOutput”。
标签: python fork subprocess stdout stderr