Python子进程(ffmpeg)只有启动一次我Ctrl-C程序? [英] Python subprocesses (ffmpeg) only start once I Ctrl-C the program?

查看:437
本文介绍了Python子进程(ffmpeg)只有启动一次我Ctrl-C程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Cygwin和Python 2.7并行运行几个ffmpeg命令。



这大概是我所拥有的:

  import subprocess 
processes = set()

commands = [ffmpeg -i input.mp4 output.avi ,ffmpeg -i input2.mp4 output2.avi]

命令中的cmd:
processes.add(
subprocess.Popen(cmd,stdout = subprocess.PIPE ,stderr = subprocess.PIPE,shell = True)


进程进程:
如果process.poll()为无:
process.wait )

现在,一旦我结束了这段代码,整个程序等待。所有的ffmpeg进程都被创建,但是它们是空闲的,即使用0%的CPU。而Python程序只是等待着。只有当我按Ctrl-C,它才突然开始编码。



我做错了什么?我必须向进程发送一些东西来启动它们吗?

解决方案

这只是一个猜测,但是 ffmpeg 通常在stderr或stdout上产生大量状态消息和输出。您正在使用 subprocess.PIPE 将stdout和stderr重定向到管道,但是您从不从中读取,因此如果管道缓冲区已满,ffmpeg进程将阻止尝试向其写入数据。



当您杀死父进程时,管道在其末端关闭,可能(我还没有检查)ffmpeg处理错误只是不要写入管道,因此被解除封锁并开始工作。



所以eiter消耗 process.stdout process.stderr 管道,或将输出重定向到 os.devnull ,如果你不在乎它。 / p>

I'm trying to run a few ffmpeg commands in parallel, using Cygwin and Python 2.7.

This is roughly what I have:

import subprocess
processes = set()

commands = ["ffmpeg -i input.mp4 output.avi", "ffmpeg -i input2.mp4 output2.avi"] 

for cmd in commands:
  processes.add(
    subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
  )

for process in processes:
  if process.poll() is None:
    process.wait()

Now, once I am at the end of this code, the whole program waits. All the ffmpeg processes are created, but they're idle, i.e., using 0% CPU. And the Python program just keeps waiting. Only when I hit Ctrl-C, it suddenly starts encoding.

What am I doing wrong? Do I have to "send" something to the processes to start them?

解决方案

This is only a guess, but ffmpeg usually produces a lot of status messages and output on stderr or stdout. You're using subprocess.PIPE to redirect stdout and stderr to a pipe, but you never read from those, so if the pipe buffer is full, the ffmpeg process will block when trying to write data to it.

When you kill the parent process the pipe is closed on its end, and probably (i haven't checked) ffmpeg handles the error by just not writing to the pipe anymore and is therefore unblocked and starts working.

So eiter consume the process.stdout and process.stderr pipes in your parent process, or redirect the output to os.devnull if you don't care about it.

这篇关于Python子进程(ffmpeg)只有启动一次我Ctrl-C程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆