简体   繁体   中英

Python loop over stdout of subprocess in chunks?

I have been iterating over the sdtout of my subprocess line by line. I was advised to do it in chunks instead. Can anyone show me how to do that please?

Line by line:

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

My attempt at chunks:

ffmpeg_sb = subprocess.Popen(ffmpegcmd, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE)
for chunk in ffmpeg_sb.stdout.read(1024):
    yield chunk
while True:
    chunk = ffmpeg_sb.stdout.read(1024)
    if len(chunk) == 0:
        break
    yield chunk

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM