NAME

Async::Group - Perl class to deal with simultaneous asynchronous calls

SYNOPSIS

use Async::Group ;
use strict ;

sub sub1 
 {
   print "Dummy subroutine \n";
   my $dummy = shift ;
   my $cb = shift ;

   &$cb(1);
 }

sub allDone
 {
   print "All done, result is ", shift ,"\n" ;
 }
my $a = Async::Group->new(name => 'aTest', test => 1) ;

$a->run(set => [ sub {&sub1( callback => sub {$a->callDone(@_)} )},
                sub {&sub1( callback => sub {$a->callDone(@_)} )} ],
       callback => \&allDone 
      )

# 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 
      )

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 call-back functions called by the aforementionned subroutines. When all these subs are finished, it calls another call-back (passed by the user) to perform whatever function required by the user.

Using fork or threads or whatever is left to the user.

Methods

new( set => [sub, sub, ...], [test => 1] )

Creates a new Async::Group object.

parameters are : - name: name of the group. The name has no special meaning but it can be helpfull for debugging. - test: will print on STDERR what's going on

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

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.

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 that can be used as a callback function by the user's routine.

So you may call run() with the following sequence :

my $cb = $a->getCbRef();
$a->run(set => [ sub {&sub1( callback => $cb)},
                 sub {&nsub1( callback => $cb )} ],
       callback => \&allDone 
      )

AUTHOR

Dominique Dumont, Dominique_Dumont@grenoble.hp.com

Copyright (c) 1998 Dominique Dumont. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

perl(1)