0

I came across a problem when using subprocess.Popen. I went through the problem here. When I add the following edit to the fake_utility.py i.e add stderr=subprocess.PIPE in subprocess.Popen:

#fake_utility.py
import time
i = 0
while True:
   print hex(i)*512
   i += 1
   time.sleep(0.5)

#python interpreter
import subprocess
proc = subprocess.Popen(['python','fake_utility.py'],stdout=subprocess.PIPE, stderr=subprocess.PIPE)
for err in proc.stderr:
    print "err::", err

for line in proc.stdout:
   print "test:", line.rstrip()

the program doesnt respond. When I press a Ctrl-C, I get the following:

Traceback (most recent call last):
File "run_fake_utility.py", line 6, in <module>
    err = proc.stderr.readline()
KeyboardInterrupt

It is being blocked in proc.stderr.readline(). If I remove the line reading from proc.stderr, the program runs fine.

What is the possible error? I am using Python2.6.

Community
  • 1
  • 1
ashokadhikari
  • 1,182
  • 3
  • 15
  • 29

1 Answers1

1

Blocking on read from STDERR would indicate that the sub-process hasn't written anything to that file-handle.

Were you actually expecting something? (i.e: if you run the same command manually from the shell and redirect STDERR - 2>file.txt - to a file, is the file non-empty?)

Otherwise, some common things that can occur:

  • You haven't yet received a newline in the STDERR output
  • You haven't yet received enough data to fill some underlying buffer
David-SkyMesh
  • 5,041
  • 1
  • 31
  • 38
  • I just wanted to print out the errors if any. I ran the command manually from the shell redirecting STDERR to the file, and it is empty. – ashokadhikari Jan 16 '13 at 05:26
  • Then it means, if there is nothing in the STDERR output, then the program blocks forever. What should be done to avoid such situation? – ashokadhikari Jan 16 '13 at 05:28
  • @liken.one use [select](http://docs.python.org/2/library/select.html) or [threads](http://docs.python.org/2/library/threading) and kill the thread in a certain time. – cyraxjoe Jan 16 '13 at 05:45