File: callback_data.pl

package info (click to toggle)
libparallel-forkmanager-perl 2.04-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 276 kB
  • sloc: perl: 479; xml: 297; makefile: 2
file content (42 lines) | stat: -rw-r--r-- 1,079 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/perl
use strict;
use warnings;

use Parallel::ForkManager;

my $max_procs = 2;
my @names = qw( Fred Jim );

my $pm = Parallel::ForkManager->new($max_procs, @ARGV);

# Setup a callback for when a child finishes up so we can
# get it's exit code and any data it collected
$pm->run_on_finish( sub {
  my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $data_structure_reference) = @_;
  print "$ident just got out of the pool ".
    "with exit code: $exit_code and data: @$data_structure_reference\n";
});

$pm->run_on_start( sub {
  my ($pid,$ident)=@_;
  print "$ident started\n";
});

foreach my $child ( 0 .. $#names ) {
  my $pid = $pm->start($names[$child]) and next;

  # This code is the child process
  # We can do here anything and obtain any data.
  # The result can be any array or hash.
  my @result = ($names[$child], length $names[$child]);
  sleep 1+rand(3);

  # pass an exit code and data stucture to finish
  $pm->finish($child, \@result );
}

#print "Waiting for Children...\n";
$pm->wait_all_children;
print "Everybody is out of the pool!\n";