我正在使用 linux/cpython 3.3/bash。这是我的问题:
#!/usr/bin/env python3
from subprocess import Popen, PIPE, DEVNULL
import time
s = Popen('cat', stdin=PIPE, stdout=DEVNULL, stderr=DEVNULL)
s.stdin.write(b'helloworld')
s.stdin.close()
time.sleep(1000) #doing stuff
这使得 cat
成为僵尸(我正忙于“做事”,不能在子进程上等待
)。在 bash 中有没有一种方法可以包装 cat
(例如,通过创建一个孙子),这将允许我写入 cat
的标准输入,但让 init 接受作为 parent ? python 解决方案也可以,我也可以使用 nohup、disown 等。
最佳答案
从另一个唯一任务是等待它的进程运行子进程。
pid = os.fork()
if pid == 0:
s = Popen('cat', stdin=PIPE, stdout=DEVNULL, stderr=DEVNULL)
s.stdin.write(b'helloworld')
s.stdin.close()
s.wait()
sys.exit()
time.sleep(1000)
关于python - 在写入其标准输入后后台进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25650055/