I have a Python script that runs three compiled programs and makes them communicate via standard I/O. This is my first time using
pexpect. My program runs very slowly. Is there anything that I can do to speed up the program? If not, I can try to do it with C/C++ pipe/fork.
import pexpect import os Checker = pexpect.spawn("./checker") # a turn based game Player = [0,1] Player = pexpect.spawn("./player1") # same game with computer player Player = pexpect.spawn("./player2") # same game with computer player # tell each player wether it is 1st or 2nd player Player.sendline("1") # Player.expect("\n") # delete input from before Player.sendline("2") Player.expect("\n") # delete input from before player = 1 while True: Player[player].expect("\n") # wait for current player's move(a line) move = Player[player].before.strip() # read current player's move print move if not Checker.isalive(): # this does not run in usual print "endgame checker died" break Checker.sendline(move) Checker.expect("\n") # delete input from before Checker.expect("\n") # wait checker's response check = Checker.before.strip() # read the response if check != "": # checker will send an empty line if move is valid and game didn't end print "endgame ", check # otherwise response is other player's number Player.kill(0) # kill all Player.kill(0) Checker.kill(0) break player = (player+1) %2 # change current player if not Player[player].isalive(): # this does not run in usual print "endgame" + player + "died" break Player[player].sendline(move) Player[player].expect("\n") # delete input from before