Skip to content

Instantly share code, notes, and snippets.

@ryn1x
Last active October 29, 2018 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryn1x/2eb896216a42a9a95590f34bfcbd8d7f to your computer and use it in GitHub Desktop.
Save ryn1x/2eb896216a42a9a95590f34bfcbd8d7f to your computer and use it in GitHub Desktop.
IPC between perl and python using stdin and stdout
#!/usr/bin/env python3
while 1:
x = input('')
if x == 'exit':
print('exiting...')
break
print(x)
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
exiting...
#!/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