Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do two sub-processes stop each other from working?

I'm having a problem getting two subprocesses to run together.

The first subprocesss is a transcode of a video stream:

subprocess.Popen("ffmpeg -i input output", shell=True)

I need this to run in the background of my program, transcoding video from my IP camera into a mjpeg stream.

The second subprocess is the Openalpr Daemon that looks at the mjpeg stream and returns car license plates it sees in the stream.

 subprocess.Popen("alprd -f", shell=True)

Here is a sample piece of python test code that tries to run both subprocesses:

import subprocess
subprocess.Popen("ffmpeg -i input output", shell=True)
subprocess.Popen("alprd -f", shell=True)

When i do this, the ffmpeg transcoding works fine, i can view the transcoded mjpeg stream and i can see ffmpegs verbose output in the console. However, the alprd daemon seems to not return any number plates as expected. In fact, i can't see any output from alprd in the console window.

If i run the above code with just one subprocess it works e.g.

import subprocess
subprocess.Popen("ffmpeg -i input output", shell=True)

works fine, as does:

import subprocess
subprocess.Popen("alprd -f", shell=True)

If i run either of the two working code snippets above - and at the same time run the other command in a separate linux terminal, it all works.

I'm clearly not understanding something with subprocesses, They are clearly conflicting with each other, but Can anyone explain what is happening and how to resolve the problem?

Thanks!

like image 904
M.Jones Avatar asked Mar 01 '19 15:03

M.Jones


People also ask

What is the function of sub process?

Subprocess in Python is a module used to run new codes and applications by creating new processes. It lets you start new applications right from the Python program you are currently writing. So, if you want to run external programs from a git repository or codes from C or C++ programs, you can use subprocess in Python.

What's the difference between a sub process and a function?

A sub performs a task but does not return a value. A function returns a value of the tasks performed. Subs can be recalled from anywhere in the program and in multiple types. Functions are called by a variable.

What is a sub process in programming?

A subprocess is a task that is created by a process to complete a task within the parent process. Let's expand on that; a process is a task performed by the program during its execution. In many, if not all cases, processes usually have smaller tasks that need to be completed before the process can end.

How do you run a subprocess in parallel Python?

We can use subprocess module to create multiple child processes and they are run in parallel. First, we search the current directory and obtain a list of all the compressed files. Next, we create a list of the sequence of program arguments, each list element corresponding to each file.


1 Answers

It's likely that ffmpeg and alprd are both trying to interact with the same stdin/stdout file handles. To solve this, create separate pipes for one or both subprocesses to use as stdin/stdout. Then they can interact with them without interfering with each other.

import subprocess

with open('ffmpeg-output.txt', 'w') as ffmpeg_output:
    ffmpeg = subprocess.Popen(
        ['ffmpeg', '-i', 'input', 'output'],
        stdin=subprocess.PIPE,
        stdout=ffmpeg_output,
        stderr=subprocess.STDOUT)
    # We won't be sending any input into ffmpeg's stdin, so close it.
    ffmpeg.stdin.close()

    # alprd inherits stdin, stdout, and stderr from the current process.
    alprd = subprocess.Popen(['alprd', '-f'])

    # Wait for the subprocesses to finish.
    ffmpeg.wait()
    alprd.wait()
like image 164
Daniel Pryden Avatar answered Oct 13 '22 22:10

Daniel Pryden