기계는 거짓말하지 않는다

Python subprocess pipe stdin write 중단 문제 본문

Python

Python subprocess pipe stdin write 중단 문제

KillinTime 2023. 8. 19. 17:03

아래와 같은 예시 코드에서

import subprocess as sp

# 실행 시킬 명령어
command = ["any command..."]

proc = sp.Popen(command, stdin=sp.PIPE, stderr=sp.PIPE)

while 반복 조건:
    # 큰 데이터 받음
    data = 데이터 IN
    # write
    proc.stdin.write(data.tostring())

반복문을 실행하다가 프로세스가 종료되는 경우가 있다.

한 가지 상황은 이미지를 데이터로 받아 FFMPEG에 raw data로 넘겨줄 때였다.

 

FFMPEG는 stderr에 지속해서 기록하므로 버퍼가 계속 차게 된다.

stderr을 사용하지 않거나, 비워주어야 한다.

비우는 방법은 write 후 stderr을 close 하거나 read 시켜줄 수 있다.

proc.stdin.write(data.tostring())
proc.stderr.close() # 또는 proc.stderr.readline()

 

opencv - Python gets stuck at pipe.stdin.write(image.tostring()) - Stack Overflow

 

Python gets stuck at pipe.stdin.write(image.tostring())

I am reading each frame of video and adding time stamp to it as given below. command = ['ffmpeg', '-y', # (optional) overwrite output file if it exists '-f', 'rawvideo', #I...

stackoverflow.com

 

Comments