Python call not terminating

Discussion of the HandBrake command line interface (CLI)
Forum rules
An Activity Log is required for support requests. Please read How-to get an activity log? for details on how and why this should be provided.
Post Reply
tarnic
Posts: 2
Joined: Mon Mar 14, 2011 4:54 pm

Python call not terminating

Post by tarnic »

Hi,
I'm using HandbrakeCLI throw Python 2.7.1 on Windows, calling a process to execute it.

If I launch it using:

Code: Select all

import subprocess
retcode = subprocess.call(command_string)
everything works fine: Handbrake works and terminates correctly.

I would like to use

Code: Select all

import subprocess
proc = subprocess.Popen(command_string,
                                stdin  = subprocess.PIPE,
                                stdout = subprocess.PIPE,
                                stderr = subprocess.PIPE,)
because I can monitor the status and the percentage.

When I launch it with this command, HandbrakeCLI is executed, works correctly, but it doesn't terminate... it stays to wait something, I don't know what.
If I check the TaskManager, during its work, the CPU of HandBrakeCLI.exe process is around 88%, correctly. When it has finished the encoding, the CPU is 0% and it doesn't do anything: the console is waiting that it terminates with a return code.

Do you know what is the problem?

Thank you!
oscaralf
Posts: 1
Joined: Tue Dec 06, 2011 12:29 am

Re: Python call not terminating

Post by oscaralf »

I've had the same problem executing a different command. In my case the problem seemed to be that you need to consume the output while it's being generated, not sure exactly why but it looks as if some kind of buffer is filled when writing to the pipes and they are not consumed, and when that happens, the subprocess.call never returns.

My solution was to have a separate thread reading the output from stdout and stderr into a variable (writing it to a file should also work, what matters is to read from those pipes). And then when the subprocess.call finishes I stop the reading thread and analyze the result.

Hope this helps
KonaBlend
Novice
Posts: 72
Joined: Tue Nov 04, 2008 2:35 am

Re: Python call not terminating

Post by KonaBlend »

try something like this to send both stdout and stderr to same pipe:

Code: Select all

p = subprocess.Popen( ..., stdout=subprocess.PIPE, stderr=subprocess.STDOUT )
d = pipe.communicate() # read all at once
for line in d:
    print( line )
or read one line at a time (likely subject to buffering, so you get pauses until a platform-specific buffer like 8 kbytes fills up):

Code: Select all

p = subprocess.Popen( ..., stdout=subprocess.PIPE, stderr=subprocess.STDOUT )
for line in p.stdout: # read one line at a time
    print( line )
p.wait() # wait for forked process to cleanup
Post Reply