ventsyv
ventsyv

Reputation: 3532

python subprocess hangs when stderr is piped

I run a java program as a subprocess but when I redirect stderr to a pipe, the program hangs. For example myProcess = subprocess.Popen(cmd, shell=True, stdout = subprocess.PIPE) works, but myProcess = subprocess.Popen(cmd, shell=True, stdout = subprocess.PIPE, stderr=subprocess.PIPE)hangs.

The command I run is something along the lines of:

java -DSomeVals -classpath <somevals> package.Name

Stderr generates massive amounts of output and I remember reading somewhere that with shell=True that might cause a deadlock. Unfortunately when i set shell=False, I get an error message that the file name is too long.

I tried myProcess = subprocess.Popen(['java', args], shell=False, stdout = subprocess.PIPE, stderr = subprocess.PIPE)but then java complains that it can't find the class I'm trying to run.

I want to pipe stderr because it's cluttering the output of my program.

Upvotes: 2

Views: 1591

Answers (1)

AtlasMeh-ed
AtlasMeh-ed

Reputation: 477

I think you just need to read from the pipe. The pipe buffer is filling up and blocking the process. Checkout this example which repeats a "HelloWorld" 50,000 times.

import subprocess
import os

def getLinesFromShellCommand(command):
    p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=os.getcwd())
    lines = []
    for curLine in p.stdout.readlines():
        lines.append(curLine)
    p.wait()
    return lines


print "Starting..."
lines = getLinesFromShellCommand("printf 'HelloWorld\n%.0s' {1..50000}")
for curLine in lines:
    print curLine,

Upvotes: 2

Related Questions