Instead of using process.communicate()
, which blocks until all data is read from the application, read the result line by line as a stream. Then, you can kill the underlying process after you've read as much as you care to. You'll also need to redirect output from ccextractorwin.exe
to STDOUT, using the flag -stdout
.
import subprocess
process = subprocess.Popen(
['ccextractorwin.exe', '-stdout', '-quiet', 'myvideo.mpg'],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)
all_output = []
while True:
out_line = process.stdout.readline()
all_output.append(out_line) # Add this line to the list of lines to keep
if out_line == u'\n': # We've hit an empty line, or whatever else you deem like a good stopping point
break # the while loop
# Now, kill the process dead in its tracks.
# This probably isn't great for open file handles, but whatever
process.kill()
This sends SIGKILL
to the application, which may (of course) work differently on windows than on Linux or OSX. If there's a problem, see here for alternative solutions on killing it: In Python 2.5, how do I kill a subprocess?
Hope that helps.