2

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.

asked Nov 13, 2010 at 11:12

2 Answers 2

6

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.

answered Nov 13, 2010 at 11:17
Sign up to request clarification or add additional context in comments.

7 Comments

You should use 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.
@Chris is right if the child process is generating a substantial amount of output.
@Zach: "Warning: Use 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?
@Chris - Only if you need to access the data as it's streamed, rather than at EOF. Note that communicate buffers in RAM, meaning if the program's output is massive, it could trigger the OOM killer or force excessive swapping. In general however, I agree with you, it was an oversight on my part.
@Zach: OK, thanks for the clarification, glad to learn more about it :-)
|
0

You might be interested in taking a look at Interprocess Communication and Networking.

answered Nov 13, 2010 at 11:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.