Twisted pipe two processes with spawnProcess

2019-07-23 17:00发布

问题:

I'm trying to use Twisted with Python2.7 for piping two processes.

What I'd like to do is:

myImagesPipesGenerator | ffmpeg -i -

myImagesPipesGenerator is outputing on stdout an infinite list of BMP images. FFmpeg is getting those pictures on stdin and encoding them in a video

So I need to:

generatorTransport = reactor.spawnProcess(myInputProtocol, "myImagesPipesGenerator", ["myImagesPipesGenerator",], env=None, childFDs={0:'w', 1:'r', 2:'r'})
ffmpegTransport = reactor.spawnProcess(myOutputProtocol, "ffmpeg", ["ffmpeg","-i","-"], env=None, childFDs={0:__What_to_use_here__, 1:'r', 2:'r'})

How to get the "output" file descriptor of generatorTransport stdout pipe, so I can use it in childFDs for ffmpegTransport?

Thanks for your help,

回答1:

Create the pipe yourself:

read, write = os.pipe()

And then pass the file descriptors where you want the children to use them. Something like:

generatorTransport = reactor.spawnProcess(..., childFDs={1: write})
ffmpegTransport = reactor.spawnProcess(..., childFDs={0: read})