Skip to content

Instantly share code, notes, and snippets.

@hoelzro
Created March 15, 2015 18:57
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 hoelzro/bd4608729d7be342ccda to your computer and use it in GitHub Desktop.
Save hoelzro/bd4608729d7be342ccda to your computer and use it in GitHub Desktop.
use v6;
use HTTP::UserAgent;
sub worker(Channel $in, Supply $out) {
for @$in -> [ $index, $url ] {
$out.emit: "$index $url";
}
say 'done';
}
my $channel = Channel.new;
my $results = Supply.new;
my @workers = (^10).map: {
start { worker($channel, $results) }
};
for ^100 -> $i {
$channel.send: [ $i, 'http://localhost:5000/index.html' ];
}
$channel.close;
await @workers;
@lizmat
Copy link

lizmat commented Mar 15, 2015

sub worker(Channel $in, Supply $out, $id) {
say "$id started";
loop {
my $work = try $in.receive;
last if !$work.defined;
$out.emit: $work;
say "$id got $work";
}
say "$id done";
}

my $channel = Channel.new;
my $results = Supply.new;

my @Workers = (^10).map: {
start { say "started worker"; worker($channel, $results, $_) }
}

say "start sending";
for ^100 -> $i {
say "sent $i";
$channel.send: [ $i, 'http://localhost:5000/index.html' ];
}
$channel.close;

await @Workers;
say "all workers done";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment