[Tutor] subprocess.Popen basics

Adam Jensen hanzer at riseup.net
Mon Oct 27 19:24:45 CET 2014


Hi, I'm exploring Popen today and I seem to be having some trouble
deciphering the [documentation][1].

[1]: docs.python.org/3/library/subprocess.html#popen-constructor

In this example (below), I expect to start a shell script as a separate
process, send a line of text through a pipe (to the shell script's
stdin) then read the shell script's response (a line of text) from
another pipe (attached to the shell script's stdout).

---------------------------------------------------------
#!/usr/bin/env python3.4

import subprocess as sp

parrot = sp.Popen(['./parrot.sh'],
        stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE,
        universal_newlines=True, start_new_session=True)

parrot.stdin.write('Pushing up daisies.\n')
print('Parrot said: ', parrot.stdout.readline())
parrot.kill()
---------------------------------------------------------

Where "parrot.sh" is:
---------------------------------------------------------
#!/bin/bash
while true
do
    read foo
    echo "Squawk: $foo"
done
---------------------------------------------------------

It hangs at the print statement and, from the sound of the fans in the
computer, I suspect it spirals off into an infinite loop somewhere /
somehow. Does anyone have any ideas about what it is that I might be
misunderstanding?



More information about the Tutor mailing list