May-09-2019, 08:40 PM
My go-to scripting language for quite some time has been perl.
However, I have been keenly interested in Python and I recently implemented a few projects in both Python 2 and 3.
I'm currently in the analysis stage of a project that requires 2-way interprocess communication with a C executable over which I have no control.
The executable is an interpreter that accepts unbuffered, newline-terminated "commands" from stdin and prints unbuffered, newline-terminated output to stdout and does not support socket communications.
The goal is to find a "minimalist" python 3 equivalent to perl's open2 function illustrated by the runnable perl script shown below
Google searching resulted in two notable options:
Any advice, pointers and/or sample code would be greatly appreciated.
Example perl code. (Should be runnable on a unix host)
The above script, not unexpectedly, produces the following output:
However, I have been keenly interested in Python and I recently implemented a few projects in both Python 2 and 3.
I'm currently in the analysis stage of a project that requires 2-way interprocess communication with a C executable over which I have no control.
The executable is an interpreter that accepts unbuffered, newline-terminated "commands" from stdin and prints unbuffered, newline-terminated output to stdout and does not support socket communications.
The goal is to find a "minimalist" python 3 equivalent to perl's open2 function illustrated by the runnable perl script shown below
Google searching resulted in two notable options:
- The multiprocessing module: After a quick look at examples it appears to me that's a bit of overkill for my use case.
- ipc from PyPI: Looks promising but I could not locate any documentation for it and it provides no documentation in the code itself.
Any advice, pointers and/or sample code would be greatly appreciated.
Example perl code. (Should be runnable on a unix host)
Quote:#!/bin/bash
perl -e '
use FileHandle;
use IPC::Open2;
# Chat with the unix "cat" utility. :)
$pid = open2(*Reader, *Writer, "cat");
print Writer "Hello\n"; # Send
print scalar(<Reader>); # Print response
print Writer "World\n"; # Send
print Writer "Thats all folks!\n";
print Writer "The End\n"; # Send
while(<Reader>) { # Print responses
print;
if (/End/) {exit}
} '
The above script, not unexpectedly, produces the following output:
Output:Hello
World
Thats all folks!
The End