Python toy process launcher

  • From: "R Dinger" <rrdinger@xxxxxxxxxx>
  • To: "programming" <programmingblind@xxxxxxxxxxxxx>
  • Date: Wed, 9 Dec 2009 16:27:27 -0800

The code follows:
---------
# runTest.py provide interface and run test.py engine

import subprocess

# launch the engine child process:
engine = subprocess.Popen("python test.py",
    universal_newlines=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)

# user interaction main loop:
while(True):
  # get a move from user:
  playerMove = raw_input("Enter a move ")

  # exit if done:
  if playerMove == 'exit':
    break

  # send player move over to engine:
  engine.stdin.write('%s\n' % playerMove)

  # get reply from engine:
  engineMove = engine.stdout.readline().strip('\n')

  # show response from engine:
  print "Engine response:>%s<" % engineMove
  # end of while loop

# termination processing:
if engine.poll() is None:
  # kill the subprocess:
  engine.kill()


 

Other related posts: