Created
March 29, 2009 13:35
-
-
Save hiratara/87396 to your computer and use it in GitHub Desktop.
pmap.pl
This file contains 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
package MyFilter; | |
use metaclass 'MooseX::POE::SweetArgs'; | |
use MooseX::POE; | |
has cont => ( | |
isa => 'ArrayRef', | |
is => 'ro', | |
required => 1, | |
); | |
sub STOP{ | |
warn __PACKAGE__ . " done\n"; | |
} | |
event exec => sub { | |
my $self = shift; | |
my ($val) = @_; | |
$POE::Kernel::poe_kernel->delay_add( | |
'exec_after' => 1 => | |
$val | |
); | |
}; | |
event exec_after => sub { | |
my $self = shift; | |
my ($val) = @_; | |
$POE::Kernel::poe_kernel->post( | |
@{ $self->cont } => | |
$val * 2 | |
); | |
}; | |
no MooseX::POE; | |
package MyPmap; | |
use metaclass 'MooseX::POE::SweetArgs'; | |
use MooseX::POE; | |
has cont => ( | |
isa => 'ArrayRef', | |
is => 'ro', | |
required => 1, | |
); | |
has filter => ( | |
isa => 'ClassName', | |
is => 'ro', | |
required => 1, | |
); | |
has count => ( | |
isa => 'Int', | |
is => 'rw', | |
); | |
has result => ( | |
isa => 'ArrayRef', | |
is => 'rw', | |
default => sub { [] }, | |
); | |
sub STOP{ | |
warn __PACKAGE__ . " done\n"; | |
} | |
event exec => sub { | |
my $self = shift; | |
my ( @args ) = @_; | |
$self->count(scalar @args); | |
for(@args){ | |
my $filter = $self->filter->new( | |
cont => [$self->get_session_id => 'recieve'], | |
); | |
$filter->yield('exec' => $_); | |
} | |
}; | |
event recieve => sub { | |
my $self = shift; | |
my ($ret) = @_; | |
$self->count($self->count - 1); | |
push @{ $self->result }, $ret; | |
if($self->count == 0){ | |
$POE::Kernel::poe_kernel->post( | |
@{ $self->cont } => | |
@{ $self->result } | |
); | |
} | |
}; | |
no MooseX::POE; | |
package MySession; | |
use metaclass 'MooseX::POE::SweetArgs'; | |
use MooseX::POE; | |
sub START{ | |
my $self = shift; | |
MyPmap->new( | |
cont => [$self->get_session_id, 'recieve'], | |
filter => 'MyFilter', | |
)->yield('exec', 1 .. 10); | |
} | |
sub STOP{ | |
warn __PACKAGE__ . " done\n"; | |
} | |
event recieve => sub { | |
my $self = shift; | |
my @odds = @_; | |
print join(",", @odds), "\n"; | |
print "done\n"; | |
}; | |
no MooseX::POE; | |
package main; | |
use strict; | |
use warnings; | |
use POE; | |
MySession->new; | |
POE::Kernel->run; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment