【问题标题】:Python loop over stdout of subprocess in chunks?Python 循环遍历子进程的标准输出?
【发布时间】:2021-05-26 14:54:21
【问题描述】:

我一直在逐行迭代子进程的 sdtout。有人建议我分块做。谁能告诉我该怎么做?

一行一行:

ffmpeg_sb = subprocess.Popen(ffmpegcmd, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE)
for stdout_line in iter(ffmpeg_sb.stdout.readline, ""):
    yield stdout_line

我对块的尝试:

ffmpeg_sb = subprocess.Popen(ffmpegcmd, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE)
for chunk in ffmpeg_sb.stdout.read(1024):
    yield chunk

【问题讨论】:

    标签: python subprocess


    【解决方案1】:
    while True:
        chunk = ffmpeg_sb.stdout.read(1024)
        if len(chunk) == 0:
            break
        yield chunk
    

    【讨论】:

      猜你喜欢
      • 2010-11-19
      • 2011-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-06
      • 1970-01-01
      • 2015-01-20
      相关资源
      最近更新 更多