-->

Making two scripts communicate

2020-07-24 04:37发布

问题:

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.

回答1:

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.



回答2:

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