Last active
August 31, 2015 02:18
-
-
Save softmoth/1da0f8e722e98c2363bf 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 perl6 | |
use v6; | |
my $output = Supply.new; | |
$output.act(*.note); | |
my @workers = do for ^5 -> $id { | |
start { | |
for ^5 -> $iter { | |
#my $proc = Proc::Async.new('echo', 'hello'); | |
my $proc = Proc::Async.new(<<env "ID=$id/$iter" "SLEEPS={ (^2).roll(3).map(*+1) }" bash -c 'x=0; echo "PROCESS $ID STARTED" >&2; for i in $SLEEPS; do echo "*** $ID #$x sleep($i)"; sleep $i; x=$((x+1)); done'>>); | |
$proc.stdout.act({ $output.emit: "[$id: $iter] " ~ $_.chomp }); | |
$proc.stderr.act({ $output.emit: "!$id: $iter! " ~ $_.chomp }); | |
#$proc.stdout.act({ note "[$id: $iter] " ~ $_.chomp }); | |
#$proc.stderr.act({ note "!$id: $iter! " ~ $_.chomp }); | |
#note "... worker $id / $iter Starting process"; | |
my $promise = $proc.start; | |
#note "... worker $id / $iter await:"; | |
#note "... worker $id / $iter end: ", \ | |
osx-await($promise); | |
} | |
"DONE $id"; | |
} | |
} | |
for @workers.kv -> $id, $w { | |
#note "### $id await:"; | |
note "### $id end: ", osx-await($w).perl; | |
} | |
$output.done; | |
note "END"; | |
exit 0; | |
# See https://github.com/ugexe/zef/blob/master/lib/Zef/Process.pm6#L76 | |
# osx bug RT125758 | |
sub osx-await(Promise $p) { | |
my $result; | |
loop { | |
state $count = 0; | |
++$count; | |
$result = $p.result; | |
#last if $result !~~ Empty; | |
last if "$result" ne ''; | |
#warn "Hit os x bug, promise '$p.perl()', count #$count" if $count > 1; | |
warn "!!!!!! GIVING UP on OS X AWAIT after $count TRIES for Promise '$p.perl()'" if $count > 25; | |
CATCH { | |
default { "Died with exception: ", $_.perl; die; } | |
} | |
} | |
# Even now it might get Empty! | |
await $p; | |
$result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment