[Tutor] Does python change subprocess based on whether it is run in the interactive shell or not
Eryk Sun
eryksun at gmail.com
Mon Oct 31 01:15:18 EDT 2022
On 10/28/22, Nathan Smith <nathan-tech at hotmail.com> wrote:
>
> p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
> stdin=subprocess.PIPE, stderr=subprocess.PIPE)
> [stdout, stderr]=p.communicate(data)
>
> Where data is some data taken from io.bytesIO.read
>
> And cmd is a command for ffmpeg like so:
>
> cmd="ffmpeg/ffmpeg.exe -f wav -ac 2 -i - -f mp3 -ab 128000 - -y"
>
> When run in an interactive shell this works like a charm. When run in a
> script file, this breaks. At first I thought it was an issue with
> FFMPEG, but now I'm wondering if python is changing something under the
> hood?
To clarify, do you mean that the subprocess.Popen instance and
communicate() call work in Python's interactive shell (i.e. the REPL),
but the same code fails in some way in a non-interactive script? Or do
you mean that running the command line in an interactive CLI shell
such as CMD works fine, but the Popen instance and communicate() call
don't work in a Python script?
If it's the latter, the main difference is that in Python you're
spawning the "ffmpeg.exe" process with its standard I/O redirected to
pipes instead of using the console. The resulting behavior can be
complicated depending on how the handles for the pipes are used by the
child process and inherited by any descendant processes that it
spawns. Calling communicate() without a timeout won't return as long
there is at least one potential writer to the stdout or stderr pipe
(e.g. some descendant process that inherited a handle to the write end
of the pipe).
More information about the Tutor
mailing list