1

I'd like to start a process, wait 2 seconds and print out whatever is in the stderr and stdout pipes so far and then exit. Here is the code I have so far and it doesn't seem to work as hoped. What am I doing wrong?

There are 3 issues:

  • The program as it stands prints out "done" then waits for the suprocess to complete before printing out the first line.
  • As it stands, the script reads one line. How to read to the end of the current buffer?
  • will the subprocess exit if the calling script exits? If so, how should I modify the function call so that the subprocess runs to completion even if the calling script exits?

    cmdStr = "./stepper.py"
    proc = subprocess.Popen(cmdStr, shell=True, bufsize=-1, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
    print "polling"
    time.sleep(2)
    print "done"
    print proc.stdout.readline()
    

Here is what the stepper.py looks like:

out = open("stepper.log", 'w')
for idx in range(3):
    time.sleep(2)
    print "Idx",idx
    sys.stdout.flush()
    out.write("%d\n"%(idx))
print "fnished"
out.write("cloing\n")
out.close()
rypel
  • 4,686
  • 2
  • 25
  • 36
fodon
  • 4,565
  • 12
  • 44
  • 58
  • 1
    For your third question, look at this post: http://stackoverflow.com/questions/6011235/run-a-program-from-python-and-have-it-continue-to-run-after-the-script-is-kille - in particular, `os.fork()` – jwd Jan 30 '12 at 18:25
  • sounds like the answer is that I"m fine if the program exits gracefully ... except for Ctrl-C condition. That is good to know. – fodon Jan 30 '12 at 18:30
  • 2
    possible duplicate of [Stop reading process output in Python without hang?](http://stackoverflow.com/questions/4417962/stop-reading-process-output-in-python-without-hang) – jfs Jan 30 '12 at 19:33

0 Answers0