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

标签 python linux python-3.x bash python-2.7

我正在尝试复制 '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/

相关文章:

python - 具有唯一电子邮件的 Django auth.user

linux - 在linux上执行另一个命令时执行一个命令

Linux/银光问题

linux - 我如何将我的 Windows 程序编译成与 Wine 一起运行的单个 Linux 二进制文件?

python - # 符号截断 URL 文本 Flask 应用程序

python - 如何使用经过训练的 Keras 模型来做出新的预测?

Python xhtml2pdf表格单元格文本显示为垂直

python - 为什么 winfo_width() 返回的按钮小部件尺寸大于其实际尺寸?

python - python 中函数的不同行为

python - 未找到符号,应在 : flat namespace 中