File Coverage

blib/lib/Parallel/Map.pm
Criterion Covered Total %
statement 15 32 46.8
branch 0 4 0.0
condition 0 5 0.0
subroutine 5 10 50.0
pod 3 3 100.0
total 23 54 42.5


line stmt bran cond sub pod time code
1             package Parallel::Map;
2              
3             our $VERSION = '0.000002'; # v0.0.2
4              
5             $VERSION = eval $VERSION;
6              
7 1     1   568 use strict;
  1         3  
  1         31  
8 1     1   6 use warnings;
  1         2  
  1         33  
9 1     1   636 use IO::Async::Function;
  1         85793  
  1         39  
10 1     1   862 use IO::Async::Loop;
  1         9762  
  1         45  
11 1     1   13 use Exporter 'import';
  1         3  
  1         397  
12              
13             our @EXPORT = qw(pmap_void pmap_scalar pmap_concat);
14              
15             sub _pmap {
16 0     0     my ($type, $code, %args) = @_;
17              
18 0 0         return "Invalid type ${type}" unless $type =~ /^(?:void|scalar|concat)$/;
19              
20 0 0         $args{concurrent} = delete $args{forks} if $args{forks};
21              
22 0   0       my $par = $args{concurrent} ||= 5;
23              
24 0           my $func = IO::Async::Function->new(code => $code);
25 0   0       $func->configure(max_workers => $par||$func->{max_workers});
26              
27 0           (my $loop = IO::Async::Loop->new)->add($func);
28              
29 0           my $fmap = Future::Utils->can("fmap_${type}");
30              
31             my $done_f = $fmap->(
32 0     0     sub { $func->call(args => [ @_ ]) },
33 0           %args,
34             );
35              
36 0           my $final_f = $loop->await($done_f);
37 0           $loop->await($func->stop);
38 0           $loop->remove($func);
39              
40 0           return $final_f->get;
41             }
42              
43 0     0 1   sub pmap_void (&;@) { _pmap void => @_ }
44 0     0 1   sub pmap_scalar (&;@) { _pmap scalar => @_ }
45 0     0 1   sub pmap_concat (&;@) { _pmap concat => @_ }
46              
47             1;
48              
49             =head1 NAME
50              
51             Parallel::Map - Multi processing parallel map code
52              
53             =head1 SYNOPSIS
54              
55             use Parallel::Map;
56            
57             pmap_void {
58             sleep 1;
59             warn "${_}\n";
60             Future->done;
61             } foreach => \@choices, forks => 5;
62              
63             =head1 DESCRIPTION
64              
65             All subroutines match L C subroutines of the same name.
66              
67             =head2 pmap_void
68              
69             pmap_void { } foreach => \@input;
70             pmap_void { } generate => sub { }
71              
72             =head2 pmap_scalar
73              
74             pmap_scalar { } foreach => \@input;
75             pmap_scalar { } generate => sub { }
76              
77             =head2 pmap_concat
78              
79             pmap_concat { } foreach => \@input;
80             pmap_concat { } generate => sub { }
81              
82             =head1 AUTHOR
83              
84             mst - Matt S. Trout (cpan:MSTROUT)
85              
86             =head1 CONTRIBUTORS
87              
88             None yet - maybe this software is perfect! (ahahahahahahahahaha)
89              
90             =head1 COPYRIGHT
91              
92             Copyright (c) 2020 the Parallel::Map L and L
93             as listed above.
94              
95             =head1 LICENSE
96              
97             This library is free software and may be distributed under the same terms
98             as perl itself.