blocks - send input to python subprocess pipeline

前端 未结 11 1328
轻奢々
轻奢々 2021-01-30 09:22

I\'m testing subprocesses pipelines with python. I\'m aware that I can do what the programs below do in python directly, but that\'s not the point. I just want to test the pipel

11条回答
  •  情歌与酒
    2021-01-30 09:39

    Responding to nosklo's assertion (see other comments to this question) that it can't be done without close_fds=True:

    close_fds=True is only necessary if you've left other file descriptors open. When opening multiple child processes, it's always good to keep track of open files that might get inherited, and to explicitly close any that aren't needed:

    from subprocess import Popen, PIPE
    
    p1 = Popen(["grep", "-v", "not"], stdin=PIPE, stdout=PIPE)
    p1.stdin.write('Hello World\n')
    p1.stdin.close()
    p2 = Popen(["cut", "-c", "1-10"], stdin=p1.stdout, stdout=PIPE)
    result = p2.stdout.read() 
    assert result == "Hello Worl\n"
    

    close_fds defaults to False because subprocess prefers to trust the calling program to know what it's doing with open file descriptors, and just provide the caller with an easy option to close them all if that's what it wants to do.

    But the real issue is that pipe buffers will bite you for all but toy examples. As I have said in my other answers to this question, the rule of thumb is to not have your reader and your writer open in the same process/thread. Anyone who wants to use the subprocess module for two-way communication would be well-served to study os.pipe and os.fork, first. They're actually not that hard to use if you have a good example to look at.

提交回复
热议问题