Async-Group
view release on metacpan or search on metacpan
my $self = shift;
return sub{$self->callDone(@_)} ;
}
sub printEvent
{
my $self = shift ;
warn "$self->{name} ",shift if $self->{test} ;
}
# Call a set of asynchronous functions which MUST have their set of user
# callbacks.. Note that the user call-back invoked when the function MUST
# call the asyncDone function with a result.
#
# When all function calls are over (i.e. all call-back were performed)
# all the returned results are logically 'anded' and the resulting result
# is passed to the main user call-back function
sub run
{
my $self = shift ;
defined $args{$_};
}
croak( "$self->name:Async::Group: set parameter is not an array ref\n")
unless ref($args{set}) eq 'ARRAY' ;
# initialize
$self->{result} = 1 ;
$self->{out} = '' ;
# compute nb of asynchronous calls that will be done
$self->{onGoing} = scalar (@{$args{set}}) ;
# make up some log message
$self->printEvent("asynCall called for ".
$self->{onGoing}." calls");
# store what to do when the asyncGroup is all done
$self->{callback} = $args{'callback'} ;
# call them
&$cb($self->{result},$self->{out});
}
}
1;
__END__
=head1 NAME
Async::Group - Perl class to deal with simultaneous asynchronous calls
=head1 SYNOPSIS
use Async::Group ;
use strict ;
sub sub1
{
print "Dummy subroutine \n";
my $dummy = shift ;
# or another way which avoids the clumsy nested subs
my $cb = $a->getCbRef();
$a->run(set => [ sub {&sub1( callback => $cb)},
sub {&nsub1( callback => $cb )} ],
callback => \&allDone
)
=head1 DESCRIPTION
If you sometimes have to launch several asynchronous calls in
parrallel and want to call one call-back function when all these calls
are finished, this module may be for you.
Async::Group is a class which enables you to call several asynchronous
routines. Each routine may have their own callback. When all the
routine are over (i.e. all their callback were called), Async::Group
will call the global callback given by the user.
Note that one Async::Group objects must be created for each group of
parrallel calls. This object may be destroyed (or will vanish itself)
once the global callback is called.
Note also that Async::Group does not perform any fork or other system
calls. It just run the passed subroutines and keep count of the
=head2 run ('set' => [ sub { } ,... ], 'callback' => sub{ ...} )
- set: array ref of a set of subroutine reference that will be called in
parrallel
- callback : global user callback function
=head2 callDone(result, [string])
Function to be called back each time an asynchronous call is finished.
When all function calls are over (i.e. all call-back were performed)
all the returned results are logically 'anded', the passed strings are
concatenated and the resulting result is passed to the global user
call-back function passed with the run() method.
=head2 getCbRef()
Syntactic sugar to avoid nested subs when defining the set of routines
that must be run in parrallel. This function will return a sub ref
Makefile.PL view on Meta::CPAN
use ExtUtils::MakeMaker;
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.
WriteMakefile(
'NAME' => 'Async::Group',
'VERSION' => '0.3',
($] >= 5.005 ?
(ABSTRACT => 'Deal with simultaneous asynchronous calls',
AUTHOR => 'Dominique Dumont (domi@barrayar.grenoble.hp.com)') : ()),
'dist' => {COMPRESS=>'gzip -9f', SUFFIX => 'gz'}
);
If you sometimes have to launch several asynchronous calls in
parrallel and want to call one call-back function when all these calls
are finished, this module may be for you.
Async::Group is a class which enables you to call several asynchronous
routines. Each routine may have their own callback. When all the
routine are over (i.e. all their callback were called), Async::Group
will call the global callback given by the user.
Note that one Async::Group objects must be created for each group of
parrallel calls. This object may be destroyed (or will vanish itself)
once the global callback is called
Note also that Async::Group does not perform any fork or other system
calls. It just run the passed subroutines and keep count of the
( run in 0.237 second using v1.01-cache-2.11-cpan-0d8aa00de5b )