script = """
print "Something to print"
while(True):
  r=raw_input()
  if r=='n':
    print "Exiting"
    break
  else :
    print "not time to break"
"""
from subprocess import PIPE, Popen

p = Popen(["python", "-u", "-c", script], stdin=PIPE, stdout=PIPE, bufsize=1)
print p.stdout.readline(), # read the first line
for i in range(10): # repeat several times to show that it works
    print >>p.stdin, i # write input
    print p.stdout.readline(), # read output

print p.communicate("n\n")[0], # signal the child to exit,
                               # read the rest of the output, 
                               # wait for the child to exit

