Python Forum

Full Version: The subprocess doesn't complete on its own.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello there.
I've got a problem with the python library "subprocess" and a ffprobe.
Environment:
OS - Ubuntu 18
RTSP link - rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov (The links fully working, I use ffprobe via a terminal and VLC player to test it)
Python 3.7.3 (default, Apr 3 2019, 19:16:38)
ffprobe version 3.4.6-0ubuntu0.18.04.1 Copyright © 2007-2019 the FFmpeg developers

My code:
import subprocess
import time
import datetime


command = ['ffprobe', '-rtsp_transport', 'tcp', '-i',
           'rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov', '-show_frames', '-select_streams', 'v',
           '-read_intervals', '%+#120']
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
count = 0
while count < 50:
    count += 1
    print('\nProcess PID = ', process.pid)
    print('Process poll = ', process.poll())
    print(datetime.datetime.now())
    time.sleep(1)
It doesn't work. The process doesn't terminate.
'print('Process poll = ', process.poll())' always prints "Process poll = None"


But that code is working:
import subprocess
import time
import datetime


command = ['ffprobe', '-rtsp_transport', 'tcp', '-i',
           'rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov', '-show_frames', '-select_streams', 'v',
           '-read_intervals', '%+#110']
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
count = 0
while count < 50:
    count += 1
    print('\nProcess PID = ', process.pid)
    print('Process poll = ', process.poll())
    print(datetime.datetime.now())
    time.sleep(1)
It works correctly.
After the 6th repetition 'print('Process poll = ', process.poll())' prints "Process poll = 0"


The different is in the ffprobe parameter (row 8) - '-read_intervals %+#110' / '-read_intervals %+#120' ('-read_intervals %+#110' means - read 110 frames and exit)

In Ubuntu terminal both commands works correctly.

But in Python the first command doesn't works.

What am I doing wrong?

P.S.
Sorry for my English.
I hope it's not a ffprobe issue.
Is there any output if you remove the stdout=subprocess.PIPE, stderr=subprocess.PIPE?
(Nov-22-2019, 02:29 PM)Gribouillis Wrote: [ -> ]Is there any output if you remove the stdout=subprocess.PIPE, stderr=subprocess.PIPE?
Yes, there is.
When I remove the "stdout=subprocess.PIPE, stderr=subprocess.PIPE" all output goes to the console. And all works correctly, the output is stopped after some seconds.
For both cases.
So I think the subprocess could get blocked because it is trying to write in a full pipe and it's waiting for someone to read on the other end of the pipe. It means that your program should read the output and error streams. Another solution if you don't care about the output is to use subprocess.DEVNULL instead of subprocess.PIPE
It looks like you're right.
If I set a file as the 'stdout' it works for any count of frames.
Thank you a lot.
You could also consider running subprocess.call() or the communicate() metĥod as alternatives to sending the output to a file.