1

I am using subprocess32 3.2.6 with Python 2.6.6 on RHEL 6.5. A sequence like:

command = "sleep 20"

proc = subprocess.Popen(command, shell=True, bufsize=-1, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

std_out, std_err = proc.communicate(None, timeout=1)

works as expected, ie the one second timeout works. However, if

command = "sleep 20; echo Hello World"

The subprocess seems to run for the entire 20 seconds. I can work around this but it would be nice to either understand what I am doing wrong or why it works the way it does. BTW, this is in a very controlled, trusted environment so the "shell=True" is not risky.

Praveen Gowda I V
  • 8,920
  • 4
  • 39
  • 49

1 Answers1

0

I tried every thing, Just use this:

def reader(f,buffer):
   while True:
     line=f.readline()
     if line:
        buffer.append(line)
     else:
        break


command = subprocess.Popen (cmd, shell=True, stdout=subprocess.PIPE,
                                                stderr=subprocess.STDOUT, stdin=subprocess.PIPE)
                    command.stdin.write (bytearray(testin, 'utf8'))
                    command.stdin.close ()
                    print ('Writing i/p')
                    linebuffer = []
                    t = Thread (target=reader, args=(command.stdout, linebuffer))
                    t.daemon = True
                    t.start ()

                    start = time.time ()
                    while start + timeout > time.time ():
                        if linebuffer:
                            ans += linebuffer.pop ()
Farhad
  • 5,598
  • 8
  • 36
  • 61
Onk_r
  • 788
  • 3
  • 18