Reading/writing to a Popen() subprocess

后端 未结 2 909
陌清茗
陌清茗 2021-02-07 20:26

I\'m trying to talk to a child process using the python subprocess.Popen() call. In my real code, I\'m implementing a type of IPC, so I want to write some data, read the respons

2条回答
  •  天命终不由人
    2021-02-07 21:24

    I would try to use Popen().communicate() if you can as it does a lot of nice things for you, but if you need to use Popen() exactly as you described, you'll need to set sed to flush its buffer after newlines with the -l option:

    p = subprocess.Popen(['sed', '-l', 's/a/x/g'],
                         stdout=subprocess.PIPE,
                         stdin=subprocess.PIPE)
    

    and your code should work fine

提交回复
热议问题