gpt4 book ai didi

python - 如何在循环中使用 os.pipe() (复制多个管道)?

转载 作者:太空宇宙 更新时间:2023-11-04 10:05:35 24 4
gpt4 key购买 nike

我正在尝试复制 'cat < hello.txt | cat | cat > hello2.txt'在 python 中仅使用 os.pipe() .我不是想做一些花哨的东西,它只是为了教育(uni)。

我真的很困惑如何做到这一点。但我正在考虑按如下方式进行:

所以我列出了要执行的管道之间的所有命令 -

pipe_commands = [['/bin/cat < hello.txt'] , ['/bin/cat'], ['/bin/cat > hello2']]

for i in pipe_command:
r, w = os.pipe()
if '<' in i:
pid = os.fork()
if pid == 0:
os.close(r)
w = os.fdopen(w, 'w') # Start Writting on write end of pipe
os.dup2(w.fileno(), sys.stdout.fileno())

f= open('hello.txt', 'r') # start reading from hello.txt
os.dup2(f.fileno(), sys.stdin.fileno())

os.execl('/bin/echo', 'echo')
else:
os.wait()
os.close(w)
r = os.fdopen(r, 'r')
os.dup2(r.fileno(), sys.stdin.fileno()) # read from read end of pipe
# OUTPUT TO BE GIVEN TO NEXT COMMAND

elif '>' in i:

pid = os.fork()

if pid == 0:

os.close(w) # Start reading from previous commands pipe output
r = os.fdopen(r, 'r')
os.dup2(r.fileno(), sys.stdin.fileno())

f = open('hello2.txt', 'w') # write to hello2.txt
os.dup2(f.fileno(), sys.stdout.fileno())

os.execl('/bin/echo', 'echo')

else:
os.wait()

else:
pid = os.fork()
if pid == 0:
os.close(r)
w = os.fdopen(w, 'w') # Start Writting on write end of pipe
os.dup2(w.fileno(), sys.stdout.fileno())

os.execl('/bin/echo', 'echo') #input from read end of the previous command
else:
os.wait()
os.close(w)
r = os.fdopen(r, 'r')
os.dup2(r.fileno(), sys.stdin.fileno()) # read from read end of pipe
# OUTPUT TO BE GIVEN TO NEXT COMMAND

这之后我很困惑,下一个命令是'/bin/cat'我该给谁做呢?从输出中读取以执行第一个管道 (cat < hello.txt | cat)?

还有关于如何将其置于循环中以使其自动化的任何提示吗? (我知道我用的for循环是错误的)

我知道这不是执行管道的最佳解决方案,但我们只被教导 os.pipe()并避免使用 os.systemos.subprocess .

提前致谢

最佳答案

看起来你的方向是正确的。

基本思想是,stdin、stdout 和/或 stderr 需要用管道替换,以便进程相互通信。由于您将多次执行此操作,因此最好为其设置一个函数。

例如

def start_process(command, newin, newout, newerr):
pid = os.fork()
if pid == 0:
# Child process
os.dup2(newin, 0) # Replace stdin
os.dup2(newout, 1) # Replace stdout
os.dup2(newerr, 2) # Replace stderr
os.execv(command, (command,))
else:
# Parent process
return pid

一旦你有了函数,你就可以开始考虑你想如何运行这些命令了。我会把循环留给你,但它类似于以下内容。

# cat < hello.txt
# stdin=hello.txt, stdout=pipe1, stderr=unchanged
hello = open('hello.txt', 'r')
pipe1_read, pipe1_write = os.pipe()
proc1 = start_process('/bin/cat', hello.fileno(), pipe1_write, sys.stderr.fileno())
os.close(pipe1_write) # only proc1 should have this

# /bin/cat
# stdin=pipe1 stdout=pipe2, stderr=unchanged
pipe2_read, pipe2_write = os.pipe()
proc2 = start_process('/bin/cat', pipe1_read, pipe2_write, sys.stderr.fileno())
os.close(pipe2_write) # only proc2 should have this

# etc...

启动所有流程后,您只需等待它们完成即可。

os.waitpid(proc1, 0)
os.waitpid(proc2, 0)
os.waitpid(proc3, 0)

关于python - 如何在循环中使用 os.pipe() (复制多个管道)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52802909/

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