gpt4 book ai didi

python - 在写入其标准输入后后台进程

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:56:23 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com