Last active
October 29, 2018 03:28
-
-
Save ryn1x/2eb896216a42a9a95590f34bfcbd8d7f to your computer and use it in GitHub Desktop.
IPC between perl and python using stdin and stdout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
while 1: | |
x = input('') | |
if x == 'exit': | |
print('exiting...') | |
break | |
print(x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 | |
1 | |
2 | |
2 | |
3 | |
3 | |
4 | |
4 | |
5 | |
5 | |
6 | |
6 | |
7 | |
7 | |
8 | |
8 | |
9 | |
9 | |
10 | |
10 | |
exiting... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl6 | |
use v6; | |
# instantiate the Proc::Async object | |
my $proc = Proc::Async.new: 'python3', 'child-proc.py', :w; | |
# instantiate a channel to store our output | |
my $chan = Channel.new; | |
# tap the Supply (stdout and stderr) to send all output to our channel | |
$proc.Supply.tap: {$chan.send: $_}; | |
# start the proc | |
my $prom = $proc.start; | |
# send a bunch of commands to the procs stdin | |
# and send the command twice to simulate not knowing how much output we get | |
for 1 .. 10 { | |
# using put because it adds a new line and doesn't alter the output | |
# not sure if UTF-8 is better or worse than binary (.write) | |
$proc.put: $_; | |
$proc.put: $_; | |
# if we know output is coming we can use .recieve | |
# it blocks until something is available in the channel and then removes/returns it | |
print $chan.receive; # using print because our output comes with a \n | |
} | |
# send the exit command for our sample program | |
$proc.put: "exit"; | |
# there is still output in our channel we have not read... | |
# this will loop until we get to something expected | |
loop { | |
if $chan.poll -> $x { | |
print $x; | |
last if $x.contains('exiting'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment