views:

20

answers:

1

When using the subprocess module, how can I do something with each line of a process's output? I don't want to wait for all the output, like when using communicate, but rather do something with it as soon as it's produced. Can this be done?

+1  A: 

Unfortunately the proposal to add this to subprocess is not yet accepted: http://www.python.org/dev/peps/pep-3145/

Here is a recipe for doing it with the existing subprocess: http://code.activestate.com/recipes/440554/

A commenter also notes this simple solution (with some downsides):

flags = fcntl.fcntl(subprocess.stdout, fcntl.F_GETFL)
fcntl.fcntl(subprocess.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK)
kanaka