fork(8) download
  1. script = """
  2. print "Something to print"
  3. while(True):
  4. r=raw_input()
  5. if r=='n':
  6. print "Exiting"
  7. break
  8. else :
  9. print "not time to break"
  10. """
  11. from subprocess import PIPE, Popen
  12.  
  13. p = Popen(["python", "-u", "-c", script], stdin=PIPE, stdout=PIPE, bufsize=1)
  14. print p.stdout.readline(), # read the first line
  15. for i in range(10): # repeat several times to show that it works
  16. print >>p.stdin, i # write input
  17. print p.stdout.readline(), # read output
  18.  
  19. print p.communicate("n\n")[0], # signal the child to exit,
  20. # read the rest of the output,
  21. # wait for the child to exit
  22.  
  23.  
Success #stdin #stdout 0.05s 9296KB
stdin
Standard input is empty
stdout
Something to print
not time to break
not time to break
not time to break
not time to break
not time to break
not time to break
not time to break
not time to break
not time to break
not time to break
Exiting