1

I am trying to execute a command as follows but it is STUCK in try block as below until the timeout kicks in,the python script executes fine by itself independently,can anyone suggest why is it so and how to debug this?

cmd = "python complete.py"
proc = subprocess.Popen(cmd.split(' '),stdout=subprocess.PIPE )
print "Executing %s"%cmd
try:
    print "In try" **//Stuck here**
    proc.wait(timeout=time_out)
except TimeoutExpired as e:
    print e
    proc.kill()
with proc.stdout as stdout:
    for line in stdout:
        print line,
charlieW
  • 53
  • 1
  • 1
  • 6
  • Depends on the details of the command, but the most likely problem is that it's sitting around trying to write to its stdout (but can't, because you're not reading from the other end of the pipeline). – Charles Duffy Oct 07 '16 at 01:06
  • 1
    This is why the docs clearly recommend you to use `.communicate()`, especially if you have redirected stdout or stderr. – Jonathon Reinhart Oct 07 '16 at 01:08
  • @Owen, I don't agree that this is a dupe of that particular question -- they're not calling `stdout.read()` here. If they were, they wouldn't have this bug. (Now, if they were redirecting stderr to a separate FIFO but only reading from stdout, *then* they'd be back in that position, but that's not the case at hand). – Charles Duffy Oct 07 '16 at 01:10

1 Answers1

1

proc.stdout isn't available to be read after the process exits. Instead, you need to read it while the process is running. communicate() will do that for you, but since you're not using it, you get to do it yourself.

Right now, your process is almost certainly hanging trying to write to its stdout -- which it can't do, because the other end of the pipe isn't being read from.

See also Using module 'subprocess' with timeout.

Community
  • 1
  • 1
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441