1

I have a python module like this:

from subprocess import *

cmdline = 'python example.py'

result = Popen(cmdline,shell=False,stdout=PIPE,stderr=PIPE,stdin=PIPE).stdout

I want the parent process to block until the child process finish executing and the parent process can receive the output of the child process. Will the code above serve my purpose? In my understanding, the child process will send its output to PIPE instead of STDOUT, and the parent process will block until the child process exits and the PIPE is closed. Can someone explain where I got misunderstood?

Another thing is either communicate or check_output returns a byte string, but I want to process the results line by line, is there a good way to do it?

ascetic652
  • 472
  • 1
  • 5
  • 18

2 Answers2

0

Instead of doing

result = Popen(cmdline,shell=False,stdout=PIPE,stderr=PIPE,stdin=PIPE).stdout`

You should do:

 stdout, stderr = Popen(cmdline,shell=False,stdout=PIPE,stderr=PIPE,stdin=PIPE).communicate()

stdout is the result you are hoping for. stderr is where the process writes the error outputs.

noteness
  • 2,440
  • 1
  • 15
  • 15
0

No, what you have done is create a Popen object called result. You need to call result.wait() or more preferably result.communicate(). More on this Here

Aaron
  • 10,133
  • 1
  • 24
  • 40