Skip to content

Instantly share code, notes, and snippets.

@oetiker
Created July 23, 2014 12:08
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 oetiker/75c7f66c178f048ac285 to your computer and use it in GitHub Desktop.
Save oetiker/75c7f66c178f048ac285 to your computer and use it in GitHub Desktop.
ForkCall callback
#!/usr/bin/env perl
use Mojo::IOLoop::ForkCall;
use Data::Dumper;
my @fc;
for my $id (1..2) {
my $fc = Mojo::IOLoop::ForkCall->new;
push @fc, $fc; #keep fc in scope
my $pid = $fc->run(
sub {
my $arg = shift;
warn "$id ($$): start sleep\n";
sleep 2;
warn "$id ($$): end sleep\n";
return ({complex=>'data',id=>$id},$arg);
},
["Scalar $id"],
);
$fc->on( finish =>
sub {
my ($fc, $err, @return) = @_;
warn "$id ($$): return: ".Dumper \@return;
warn "$id ($$): error: $err\n" if $err;
}
);
};
Mojo::IOLoop->start;
1 (7817): start sleep
1 (7818): return: $VAR1 = [];
1 (7818): error: Magic number checking on storable string failed at /usr/lib/perl/5.14/Storable.pm line 416, at /home/oetiker/checkouts/znapzend/bin/../thirdparty/lib/perl5/Mojo/IOLoop/ForkCall.pm line 80
2 (7818): start sleep
1 (7817): end sleep
2 (7818): end sleep
1 (7816): return: $VAR1 = [
{
'complex' => 'data',
'id' => '1'
},
'Scalar 1'
];
2 (7816): return: $VAR1 = [
{
'complex' => 'data',
'id' => '2'
},
'Scalar 2'
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment