I have to make two programs (for example a "script A" (.py) and "script B"(.exe)) communicate. Both programs are on a infinite loop: Script A needs to write to the stdin of script B and afterwards read the stdout of script B thereafter write again etc. Script B I cannot change.
Both files are on my hard disk, so I there must be a better way to solve this than networking. I can, however, write files with script A. This is not course homework, I am writing a GUI for a game and I have a few AI's preprogrammed. I have thought of piping (python scripta.py | scriptb.exe), but that seemed to require script A to finish before script B could execute. Then again, as I've never used piping, I might have missed something obvious.
I would prefer if the tools needed would be part of standard library, but if they're not, too bad.
The solution would have to work on both Linux and Windows. Could any of you point me in the right direction? Thank you for your time.
2 Answers 2
If you start "Script B" from within "script A" using the subprocess module, you will be able to directly interact with its stdin and stdout. For example:
from subprocess import Popen, PIPE
prog = Popen("scriptA.exe", shell=True, stdin=PIPE, stdout=PIPE)
prog.stdin.write("This will go to script A\n")
print prog.stdout.read()
prog.wait() # Wait for scriptA to finish
Just be careful, as calls to read will block, meaning if the script doesn't have anything to print, the call will hang until it does. The easiest way to avoid this is to use threading.
7 Comments
print prog.communicate('This will go to script A')[0] rather than using prog.stdin and prog.stdout directly - read the subprocess module documentation warning about not using stdin and stdout directly.communicate() rather than .stdin.write, .stdout.read or .stderr.read to avoid deadlocks due to any of the other OS pipe buffers filling up and blocking the child process." I would have thought it's best practice to use communicate absolutely always; I haven't used it with anything larger than a few hundred bytes, but I use communicate and it works well - is there a reason why you used .stdin.write and .stdout.read directly?You might be interested in taking a look at Interprocess Communication and Networking.