-
-
Save gryftir/6182256 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 perl | |
# ABSTRACT: proof of concept for sending subs to a child process for evaluation | |
use strict; | |
use warnings; | |
use v5.10; | |
use Data::Dumper; | |
use Socket; | |
use Time::HiRes qw(sleep); | |
$|=1; | |
my %funcs = map { | |
s/\A\s+|\s+\z//g; | |
my ($name) = /^\s*sub\s+([[:alnum:]]+)/ | |
or die "Failed to find name in $_"; | |
$name => $_; | |
} | |
grep /\S/, | |
split /^\s*$/m, | |
do { local $/; <DATA> }; | |
say Dumper(\%funcs); | |
pipe READER, WRITER | |
or die "pipe: $!"; | |
WRITER->autoflush; | |
if (my $pid = fork) { | |
my $count = 0; | |
for my $fn (sort keys %funcs) { | |
say "$$ parent send $fn"; | |
(my $source = $funcs{$fn}) =~ s/\n/ /g; | |
print WRITER $source, "\n"; | |
printf WRITER "%s(%s)\n", $fn, ++$count; | |
} | |
sleep 1; | |
} else { | |
while (<READER>) { | |
chomp; | |
say "$$ child read $_"; | |
eval $_; | |
} | |
} | |
say "done"; | |
__DATA__ | |
sub func1 { | |
sleep 0.1; say "Hello from func1 (@_)"; | |
} | |
sub func2 { | |
sleep 0.1; say "Hello from func2 (@_)"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment